2-8 R语言基础 日期与时间

2020-09-16 15:21:50 浏览数 (2)

#日期 Date

> x<-date() > class(x) [1] "character"

> x2 <- Sys.Date() > class(x2) [1] "Date"

> x3<-as.Date("2018-11-03") > class(x3) [1] "Date"

> weekdays(x3) #星期 [1] "星期六"

> months(x3) #月份 [1] "十一月"

> quarters(x3) #季度 [1] "Q4"

> julian(x3) #距离1970-01-01有多少天 [1] 17838 attr(,"origin") [1] "1970-01-01"

> x4 <- as.Date("2018-11-01") > x4-x3 Time difference of -2 days

> as.numeric(x4-x3) [1] -2

> #时间 Time > x <- Sys.time() > class(x) [1] "POSIXct" "POSIXt"

> y <- as.POSIXct(x) > y [1] "2018-11-11 15:07:57 CST"

> z <- as.POSIXlt(x) > z

[1] "2018-11-11 15:07:57 CST"

> names(unclass(z)) [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" [10] "zone" "gmtoff"

> z$sec [1] 57.64549

> as.POSIXct(z) [1] "2018-11-11 15:07:57 CST"

> as.Date("2018-01-01") [1] "2018-01-01" > x1 <- "Jan 1,2018 01:01"

> strptime(x1,"%B %d, %Y %H:%M") [1] NA

0 人点赞