当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R weekdays 提取 POSIXt 或日期对象的部分内容


R语言 weekdays 位于 base 包(package)。

说明

提取工作日、月份或季度或儒略时间(自某个来源以来的天数)。这些是通用函数:内部日期时间类的方法记录在此处。

用法

weekdays(x, abbreviate)
## S3 method for class 'POSIXt'
weekdays(x, abbreviate = FALSE)
## S3 method for class 'Date'
weekdays(x, abbreviate = FALSE)

months(x, abbreviate)
## S3 method for class 'POSIXt'
months(x, abbreviate = FALSE)
## S3 method for class 'Date'
months(x, abbreviate = FALSE)

quarters(x, abbreviate)
## S3 method for class 'POSIXt'
quarters(x, ...)
## S3 method for class 'Date'
quarters(x, ...)

julian(x, ...)
## S3 method for class 'POSIXt'
julian(x, origin = as.POSIXct("1970-01-01", tz = "GMT"), ...)
## S3 method for class 'Date'
julian(x, origin = as.Date("1970-01-01"), ...)

参数

x

从类 "POSIXt""Date" 继承的对象。

abbreviate

逻辑向量(可能被回收)。名字应该缩写吗?

origin

从类 "POSIXt""Date" 继承的长度为 1 的对象。

...

其他方法的参数。

weekdaysmonths 返回正在使用的语言环境中的名称字符向量,即 Sys.getlocale("LC_TIME")

quarters"Q1" 的字符向量返回到 "Q4"

julian返回自原点以来的天数(可能是小数),原点为"origin"属性。所有时间计算在R已忽略leap-seconds。

注意

其他组件(例如月份或年份)非常容易计算:只需使用 as.POSIXlt 并提取相关组件即可。或者(特别是如果需要将组件作为字符串),请使用 strftime

例子

## first two are locale dependent:
weekdays(.leap.seconds)
months  (.leap.seconds)
quarters(.leap.seconds)

## Show how easily you get month, day, year, day (of {month, week, yr}), ... :
## (remember to count from 0 (!): mon = 0..11, wday = 0..6,  etc !!)

##' Transform (Time-)Date vector  to  convenient data frame :
dt2df <- function(dt, dName = deparse(substitute(dt))) {
    DF <- as.data.frame(unclass(as.POSIXlt( dt )))
    `names<-`(cbind(dt, DF, deparse.level=0L), c(dName, names(DF)))
}
## e.g.,
dt2df(.leap.seconds)    # date+time
dt2df(Sys.Date() + 0:9) # date

##' Even simpler:  Date -> Matrix - dropping time info {sec,min,hour, isdst}
d2mat <- function(x) simplify2array(unclass(as.POSIXlt(x))[4:7])
## e.g.,
d2mat(seq(as.Date("2000-02-02"), by=1, length.out=30)) # has R 1.0.0's release date


## Julian Day Number (JDN, https://en.wikipedia.org/wiki/Julian_day)
## is the number of days since noon UTC on the first day of 4317 BCE.
## in the proleptic Julian calendar.  To more recently, in
## 'Terrestrial Time' which differs from UTC by a few seconds
## See https://en.wikipedia.org/wiki/Terrestrial_Time
julian(Sys.Date(), -2440588) # from a day
floor(as.numeric(julian(Sys.time())) + 2440587.5) # from a date-time

也可以看看

DateTimeClassesDateSys.getlocale("LC_TIME") 对于 months()weekdays() 至关重要。

相关用法


注:本文由纯净天空筛选整理自R-devel大神的英文原创作品 Extract Parts of a POSIXt or Date Object。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。