lubridate包主要有两类函数,一类是处理时点数据(time instants),另一类是处理时段数据(time spans)。
时段类函数,它可以处理三类对象,分别是:
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
times <- now()
am(times)
## [1] TRUE
pm(times)
## [1] FALSE
# difftime
diff <- make_difftime(days = 31)
as.interval(diff, ymd("2009-01-01"))
## [1] 2009-01-01 UTC--2009-02-01 UTC
as.interval(diff, ymd("2009-02-01"))
## [1] 2009-02-01 UTC--2009-03-04 UTC
# duration
dur <- duration(days = 31)
as.interval(dur, ymd("2009-01-01"))
## [1] 2009-01-01 UTC--2009-02-01 UTC
as.interval(dur, ymd("2009-02-01"))
## [1] 2009-02-01 UTC--2009-03-04 UTC
# period
per <- period(months = 1)
as.interval(per, ymd("2009-01-01"))
## [1] 2009-01-01 UTC--2009-02-01 UTC
as.interval(per, ymd("2009-02-01"))
## [1] 2009-02-01 UTC--2009-03-01 UTC
# numeric
as.interval(3600, ymd("2009-01-01"))
## [1] 2009-01-01 UTC--2018-11-10 UTC
# interval
span <- interval(as.POSIXct("2009-01-01"), as.POSIXct("2010-02-02 01:01:01"))
span
## [1] 2009-01-01 CST--2010-02-02 01:01:01 CST
as.period(span)
## [1] "1y 1m 1d 1H 1M 1S"
as.period(span, units = "day")
## [1] "1y 1m 1d 1H 1M 1S"
leap <- interval(ymd("2016-01-01"), ymd("2017-01-01"))
as.period(leap, unit = "days")
## [1] "366d 0H 0M 0S"
as.period(leap, unit = "years")
## [1] "1y 0m 0d 0H 0M 0S"
dst <- interval(ymd("2016-11-06", tz = "America/Chicago"), ymd("2016-11-07",
tz = "America/Chicago"))
as.period(dst, unit = "hours")
## [1] "25H 0M 0S"
dt_utc <- ymd_hms("2010-08-03 00:50:50")
dt_europe <- ymd_hms("2010-08-03 00:50:50", tz = "Europe/London")
c(as_date(dt_utc), as.Date(dt_utc))
## [1] "2010-08-03" "2010-08-03"
## [1] '2010-08-03' '2010-08-03'
c(as_date(dt_europe), as.Date(dt_europe))
## [1] "2010-08-03" "2010-08-02"
## [1] '2010-08-03' '2010-08-02' need not suply origin
as_date(10)
## [1] "1970-01-11"
## [1] '1970-01-11'
x <- as.POSIXct("2012-03-26 23:12:13", tz = "Etc/GMT+8")
date(x)
## [1] "2012-03-26"
as.Date(x) # by default as.Date assumes you want to know the date in UTC
## [1] "2012-03-27"
as.Date(x, tz = "Etc/GMT+8")
## [1] "2012-03-26"
date(x) <- as.Date("2000-01-02")
x
## [1] "2000-01-02 23:12:13 GMT+8"
date <- as.POSIXlt("2009-02-10 08:07:43 CST")
date
## [1] "2009-02-10 08:07:43 CST"
year(date) #获取年份
## [1] 2009
month(date) #获取月份
## [1] 2
day(date)
## [1] 10
mday(date)
## [1] 10
hour(date)
## [1] 8
minute(date)
## [1] 7
second(date)
## [1] 43
update(date, year = 2010, month = 1, mday = 1)
## [1] "2010-01-01 08:07:43 CST"
update(date, year = 2010, month = 13, mday = 1)
## [1] "2011-01-01 08:07:43 CST"
update(date, minute = 10, second = 3)
## [1] "2009-02-10 08:10:03 CST"
date <- ymd("2009-02-10")
decimal <- decimal_date(date)
decimal
## [1] 2009.11
date_decimal(decimal)
## [1] "2009-02-10 UTC"
获取天数:
设置天数:
x <- as.Date("2016-07-08")
day(x)
## [1] 8
wday(x) #周是从周日至周六算,一周的第几天,即星期数
## [1] 6
wday(ymd(160708)) #默认使用数字表示星期
## [1] 6
wday(ymd(160708), label = TRUE, abbr = FALSE) #使用星期全称
## [1] Friday
## 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday
wday(ymd(160708), label = TRUE, abbr = TRUE) #使用星期简称
## [1] Fri
## Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat
wday(ymd(160708) + days(-2:4), label = TRUE, abbr = TRUE)
## [1] Wed Thurs Fri Sat Sun Mon Tues
## Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat
yday(ymd(160301))
## [1] 61
yday(ymd(150301))
## [1] 60
mday(x)
## [1] 8
qday(x)
## [1] 8
# 设置:
yday(x) <- 1
x
## [1] "2016-01-01"
day(x) <- 366
x
## [1] "2016-12-31"
mday(x) > 3
## [1] TRUE
x <- as.Date("2016-07-08")
days_in_month(x)
## Jul
## 31
date <- ymd("2009-02-10")
(date)
## [1] "2009-02-10"
duration(day = -1)
## [1] "-86400s (~-1 days)"
duration(90, "seconds")
## [1] "90s"
duration(1.5, "minutes")
## [1] "90s"
duration(-1, "days")
## [1] "-86400s (~-1 days)"
duration(second = 90)
## [1] "90s"
duration(minute = 1.5)
## [1] "90s"
duration(mins = 1.5)
## [1] "90s"
duration(second = 3, minute = 1.5, hour = 2, day = 6, week = 1)
## [1] "1130493s (~13.08 days)"
duration(hour = 1, minute = -60)
## [1] "0s"
is.duration(as.Date("2009-08-03"))
## [1] FALSE
is.duration(duration(days = 12.4))
## [1] TRUE
here()
## [1] "2016-07-12 08:52:44 CST"
now()
## [1] "2016-07-12 08:52:44 CST"
hm(c("09:10", "09:02", "1:10"))
## [1] "9H 10M 0S" "9H 2M 0S" "1H 10M 0S"
hm("7 6")
## [1] "7H 6M 0S"
hm("6,5")
## [1] "6H 5M 0S"
x <- c("09:10:01", "09:10:02", "09:10:03")
hms(x)
## [1] "9H 10M 1S" "9H 10M 2S" "9H 10M 3S"
hms("7 6 5", "3:23:::2", "2 : 23 : 33", "Finished in 9 hours, 20 min and 4 seconds")
## [1] "7H 6M 5S" "3H 23M 2S" "2H 23M 33S" "9H 20M 4S"
x <- ymd("2012-03-26")
hour(x)
## [1] 0
hour(x) <- 1
minute(x) <- 20
second(x) <- 2
x
## [1] "2012-03-26 01:20:02 UTC"
make_datetime(year = 1970, month = 1L, day = 1L, hour = 0, min = 0, sec = 0, tz = “UTC”)
dates <- now() + days(1:10)
make_datetime()
## [1] "1970-01-01 UTC"
make_datetime(year = 1999, month = 12, day = 22, sec = c(10, 11))
## [1] "1999-12-22 00:00:10 UTC" "1999-12-22 00:00:11 UTC"
make_difftime(1)
## Time difference of 1 secs
make_difftime(60)
## Time difference of 1 mins
make_difftime(3600)
## Time difference of 1 hours
make_difftime(3600, units = "minute")
## Time difference of 60 mins
make_difftime(second = 90)
## Time difference of 1.5 mins
make_difftime(minute = 1.5)
## Time difference of 1.5 mins
make_difftime(second = 3, minute = 1.5, hour = 2, day = 6, week = 1)
## Time difference of 13.08441 days
make_difftime(hour = 1, minute = -60)
## Time difference of 0 secs
make_difftime(day = -1)
## Time difference of -1 days
make_difftime(120, day = -1, units = "minute")
## Time differences in mins
## [1] 2 -1440
dmy(10216)
## [1] "2016-02-01"
mdy(1022016)
## [1] "2016-01-02"
ymd("16/02/29")
## [1] "2016-02-29"
x <- c("09-01-01", "09-01-02", "09-01-03")
ymd(x)
## [1] "2009-01-01" "2009-01-02" "2009-01-03"
x <- c("2009-01-01", "2009-01-02", "2009-01-03")
ymd(x)
## [1] "2009-01-01" "2009-01-02" "2009-01-03"
ymd(90101, 90102)
## [1] "2009-01-01" "2009-01-02"
dmy(10210)
## [1] "2010-02-01"
mdy(10210)
## [1] "2010-01-02"
x <- c(20090101, "2009-01-02", "2009 01 03", "2009-1-4", "2009-1, 5", "Created on 2009 1 6",
"200901 !!! 07", "2016/1/04")
ymd(x)
## [1] "2009-01-01" "2009-01-02" "2009-01-03" "2009-01-04" "2009-01-05"
## [6] "2009-01-06" "2009-01-07" "2016-01-04"