前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言包_lubridate

R语言包_lubridate

作者头像
用户1147754
发布2019-05-26 12:08:48
1.2K0
发布2019-05-26 12:08:48
举报
文章被收录于专栏:YoungGyYoungGy
  • Parsing dates and times
  • Setting and Extracting information
  • Time Zones
  • Time Intervals
  • Arithmetic with date times
  • Others
  • Vectorization
  • 参考资料

Parsing dates and times

代码语言:javascript
复制
install.packages("lubridate")
library(lubridate)
ymd("20150904")
ymd("2015-9-4")
ymd("2015/9/04")
dmy("04/09/15")
ymd_hms("2011-06-04 12:00:00")
x = ymd_hms("2011-08-10 14:05:08", tz = "Pacific/Auckland")
class(x)

Setting and Extracting information

代码语言:javascript
复制
second(x)
minute(x)
hour(x)
day(x)
month(x)
year(x)
wday(x,label = T)
yday(x)
week(x)

Time Zones

代码语言:javascript
复制
#奥克兰9点电话会议,求芝加哥几点开
meeting <- ymd_hms("2011-07-01 09:00:00", tz = "Pacific/Auckland")
with_tz(meeting, "America/Chicago")

#芝加哥如果误认为9点开,那么奥克兰接到电话的时间
mistake <- force_tz(meeting, "America/Chicago")
with_tz(mistake, "Pacific/Auckland")

Time Intervals

代码语言:javascript
复制
arrive <- ymd_hms("2011-06-04 12:00:00", tz = "Pacific/Auckland")
leave <- ymd_hms("2011-08-10 14:00:00", tz = "Pacific/Auckland")
auckland <- interval(arrive, leave)
auckland <- arrive %--% leave
auckland
jsm <- interval(ymd(20110720, tz = "Pacific/Auckland"), ymd(20110831, tz = "Pacific/Auckland"))
#if trival overlap
auckland
jsm
lubridate::setdiff(auckland, jsm)

Other functions that work with intervals include int_start, int_end, int_flip, int_shift, int_aligns, union, intersect, setdiff, and %within%.

Arithmetic with date times

两种类型的时间数据: - period - duration

需要两种类型的时间数据的原因是:时间并不是精准的。 例如闰年366天。period为366,但是duration为365.

代码语言:javascript
复制
# period
minutes(2)
years(1)
# duration
dminutes(2)
dyears(1)   #always 365 days
# regular year
leap_year(2011)
leap_year(2000)

ymd(20110101) + dyears(1)
## [1] "2012-01-01 UTC"
ymd(20110101) + years(1)
## [1] "2012-01-01 UTC"
leap_year(2012)  ## leap year
## [1] TRUE
ymd(20120101) + dyears(1)
## [1] "2012-12-31 UTC"
ymd(20120101) + years(1)
## [1] "2013-01-01 UTC"

关于时间取整,取模的其他计算

代码语言:javascript
复制
meeting <- ymd_hms("2011-07-01 09:00:00", tz = "Pacific/Auckland")
meetings <- meeting + weeks(0:5)
jsm <- interval(ymd(20110720, tz = "Pacific/Auckland"), ymd(20110831, tz = "Pacific/Auckland"))
meeting
meetings
jsm
meetings %within% jsm

arrive <- ymd_hms("2011-06-04 12:00:00", tz = "Pacific/Auckland")
leave <- ymd_hms("2011-08-10 14:00:00", tz = "Pacific/Auckland")
auckland <- interval(arrive, leave)
auckland
#days i stay
auckland/edays(1)
#minutes i stay
auckland/eminutes(1)

auckland%/%months(1)
auckland%%months(1)
as.period(auckland%%months(1))
as.period(auckland)

Others

代码语言:javascript
复制
jan31 <- ymd("2013-01-31")
jan31 + months(0:11)

floor_date(jan31, "month") + months(0:11) + days(31)

jan31 %m+% months(0:11)

Vectorization

代码语言:javascript
复制
last_day <- function(date) {
    ceiling_date(date, "month") - days(1)
}
last_day(jan31)

参考资料

lubridate

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年09月05日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Parsing dates and times
  • Setting and Extracting information
  • Time Zones
  • Time Intervals
  • Arithmetic with date times
  • Others
  • Vectorization
  • 参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档