Installation/安装
# The easiest way to get lubridate is to install the whole tidyverse:
install.packages("tidyverse")
# Alternatively, install just lubridate:
install.packages("lubridate")
# Or the the development version from GitHub:
# install.packages("devtools")
devtools::install_github("tidyverse/lubridate")
Features / 特征
- Easy and fast parsing of date-times: ymd(), ymd_hms, dmy(), dmy_hms, mdy(), ...
- 快速容易解析日期
ymd(20101215)
#> [1] "2010-12-15"
mdy("4/1/17")
#> [1] "2017-04-01"
- Simple functions to get and set components of a date-time, such as year(), month(), wday(), hour(), minute() and second():
- 用简单函数处理日期,获得年,月,日,星期几......
day <- dmy("14/10/1979")
month(bday)
#> [1] 10
wday(bday, label = TRUE)
#> [1] Sun
#> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
#周数计算
wday <- wday(time)-1
year(bday) <- 2016
#> [1] Fri
#> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
- Helper functions for handling time zones: with_tz(), force_tz()
- 利用函数处理时区
time <- ymd_hms("2010-12-13 15:30:30")
time
#> [1] "2010-12-13 15:30:30 UTC"
# Changes printing
with_tz(time, "America/Chicago")
#> [1] "2010-12-13 09:30:30 CST"
# Changes time
force_tz(time, "America/Chicago")
#> [1] "2010-12-13 15:30:30 CST"
补充 : tz:提取时间数据的时区
with_tz:将时间数据转换为另一个时区的同一时间
force_tz:将时间数据的时区强制转换为另一个时区
# 输入欧洲杯决赛在乌克兰的开场时间,再转为北京时间
eurotime <- ymd_hms('2012-07-01 21:45:00',tz='EET')
with_tz(eurotime,tzone='asia/shanghai')
Lubridate also expands the type of mathematical operations that can be performed with date-time objects. It introduces three new time span classes borrowed from http://joda.org.
Lubridate还扩展了可以使用日期时间对象执行的数学运算 。 它介绍了三个时间跨度主要借鉴于 http://joda.org.
- durations, which measure the exact amount of time between two points
- 持续时间,测量连个时间点间长度
- periods, which accurately track clock times despite leap years, leap seconds, and day light savings time
- 准确跟踪时钟频率
- intervals, a protean summary of the time information between two points
- 间隔,两点之间时间信息总结
补充:时段类函数,它可以处理三类对象,分别是:
interval:最简单的时段对象,它由两个时点数据构成。
duration:去除了时间两端的信息,纯粹以秒为单位计算时段的长度,不考虑闰年和闰秒,它同时也兼容基本包中的difftime类型对象。
period:以较长的时钟周期来计算时段长度,它考虑了闰年和闰秒,适用于长期的时间计算。以2012年为例,duration计算的一年是标准不变的365天,而period计算的一年就会变成366天。
网友评论