数据科学与R语言数据-R语言-图表-决策-Linux-PythonR语言

字符串处理

2018-10-22  本文已影响2人  Liam_ml

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 / 特征

ymd(20101215)
#> [1] "2010-12-15"
mdy("4/1/17")
#> [1] "2017-04-01"
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
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.

补充:时段类函数,它可以处理三类对象,分别是:

interval:最简单的时段对象,它由两个时点数据构成。

duration:去除了时间两端的信息,纯粹以秒为单位计算时段的长度,不考虑闰年和闰秒,它同时也兼容基本包中的difftime类型对象。

period:以较长的时钟周期来计算时段长度,它考虑了闰年和闰秒,适用于长期的时间计算。以2012年为例,duration计算的一年是标准不变的365天,而period计算的一年就会变成366天。

上一篇下一篇

猜你喜欢

热点阅读