本节介绍一些关于时间的函数,我觉得不是重点。。

> d1 <- Sys.Date()
> d1
[1] "2017-04-13"
> class(d1)
[1] "Date"
> unclass(d1)
[1] 17269
> d2 <- as.Date('1969-01-01')
> unclass(d2) # 之所以是-365的原因是因为日期是从1970-01-01开始,1969-01-01是1970-01-01的365天前
[1] -365

> t1 <- Sys.time()
> t1
[1] "2017-04-13 17:35:15 CST"
> class(t1)
[1] "POSIXct" "POSIXt" 
> unclass(t1)
[1] 1492076115
> t2 <- as.POSIXlt(Sys.time())
> class(t2)
[1] "POSIXlt" "POSIXt" 
> t2
[1] "2017-04-13 17:37:28 CST"
> unclass(t2)
$sec
[1] 28.75808
$min
[1] 37
$hour
[1] 17
$mday
[1] 13
$mon
[1] 3
$year
[1] 117
$wday
[1] 4
$yday
[1] 102
$isdst
[1] 0
$zone
[1] "CST"
$gmtoff
[1] 28800
attr(,"tzone")
[1] ""    "CST" "CST"

> str(unclass(t2))
List of 11
 $ sec   : num 28.8
 $ min   : int 37
 $ hour  : int 17
 $ mday  : int 13
 $ mon   : int 3
 $ year  : int 117
 $ wday  : int 4
 $ yday  : int 102
 $ isdst : int 0
 $ zone  : chr "CST"
 $ gmtoff: int 28800
 - attr(*, "tzone")= chr [1:3] "" "CST" "CST"
 > t2$min
[1] 37
> weekdays(d1)
[1] "星期四"
> months(d1)
[1] "四月"
> quarters(t2) # 季度,一共四个季度Q1~Q4
[1] "Q2"
> t3 <- "02/27/92 22:29:56"
> strptime(t3, "%m/%d/%y %H:%M:%S")
[1] "1992-02-27 22:29:56 CST

> Sys.time() - t1
Time difference of 8.792548 mins
> difftime(Sys.time(), t1, units = 'days') 
Time difference of 0.006238172 days

POSIXct is just one of two ways that R represents time information.POSIXt可以不管。

Dates are represented by the 'Date' class and times are represented by the 'POSIXct' and 'POSIXlt' classes. Internally, dates are stored as the number of days since 1970-01-01 and times are stored as either the number of seconds since 1970-01-01 (for 'POSIXct') or a list of seconds, minutes, hours, etc. (for 'POSIXlt').

results matching ""

    No results matching ""