base包中的函数
#将字符串转换为时间
d=as.Date("2015-5-6")
d
#获取月份
months(d)
#获取星期
weekdays(d)
结果:
> d
[1] "2015-05-06"
> months(d)
[1] "五月"
> weekdays(d)
[1] "星期三"
使用lubridate包
如何提取季节,月份,使用lubridate包
年份:== year(datae)==
月份: month(datae)
日期:== day(datae)==
季节:== quarter(datae)==
还有其他有用的函数
代码
library(lubridate)
d<-c("2012-1-10","2013-5-9","2014-6-25")
year(d)
month(d)
day(d)
quarter(d)
结果
> library(lubridate)
> d<-c("2012-1-10","2013-5-9","2014-6-25")
> year(d)
[1] 2012 2013 2014
> month(d)
[1] 1 5 6
> day(d)
[1] 10 9 25
> quarter(d)
[1] 1 2 2
————————————————
版权声明:本文为CSDN博主「育种数据分析之放飞自我」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yijiaobani/article/details/89360640
处理时间可以用hms
包
hms包,用于存储和格式化时间,基于difftime类型,使用S3的面向对象数据结构。
函数列表:
hms: 创建一个hms类型对象
is.hms: 判断是否是hms类型
parse_hm: 解析hm值
parse_hms: 解析hms值
round_hms:四舍五入对齐
trunc_hms:裁剪对齐
as.hms: hms泛型函数,S3类型,用于隐式调用它的继承函数
as.hms.character: character转型hms,用于as.hms的继承调用
as.hms.default: hms转型hms,用于as.hms的继承调用
as.hms.difftime:difftime转型hms,用于as.hms的继承调用
as.hms.numeric: numeric转型hms,用于as.hms的继承调用
as.hms.POSIXlt: POSIXlt转型hms,用于as.hms的继承调用
as.hms.POSIXt: POSIXt转型hms,用于as.hms的继承调用
as.character.hms: hms转型character,用于as.character的继承调用
as.data.frame.hms: hms转型data.frame,用于as.data.frame的继承调用
as.POSIXct.hms: hms转型POSIXct,用于as.POSIXct的继承调用
as.POSIXlt.hms: hms转型POSIXlt,用于as.POSIXlt的继承调用
format.hms: 格式化hms,用于format的继承调用
print.hms: 打印hms对像,用于print的继承调用
从函数列表可以看到,hms包的功能很单一,就在做数据类型和数据变型,是底层的数据结构包,设计思路与zoo包的设计思路一致。
hms包中,有大量的as.xxx()函数、format.hms()函数和print.hms()函数,是被用于S3类型函数继承调用的,是不需要我们在写程序的时候显示调用的。
例如:
hms()函数,用于创建一个hms类型的对象。
函数定义:
hms(seconds = NULL, minutes = NULL, hours = NULL, days = NULL)
hms()函数,接收4个参数,分别对应秒,分,时,日。
创建hms对象
创建12:34:56的时间对象
a1<-hms(56, 34, 12);a1
12:34:56
创建 10日12:34:56的时间对象
a2<-hms(56, 34, 12,10);a2
252:34:56
打印结果的第一位252=10*24+12。
== 把字符串转hms==
as.hms('19:13:14')
19:13:14
非法时间字符串转型
as.hms('19:78:14')
NA
as.xxx.hms:把hms转型到其他类型
默认转型
as.hms(a1)
12:34:56
hms转型character,实际会隐式调用as.character.hms()函数
as.character(a1)
[1] "12:34:56"
hms转型POSIXct
as.POSIXct(a1)
[1] "1970-01-01 12:34:56 UTC"
hms转型POSIXlt
as.POSIXlt(a1)
[1] "1970-01-01 12:34:56 UTC"
parse_hms对字符串进行转型,对比parse_hms()与as.hms()结果一样的。
执行parse_hms
parse_hms("12:34:56.789")
12:34:56.789
as.hms("12:34:56.789")
12:34:56.789
执行parse_hm
parse_hm("12:34")
12:34:00
as.hms("12:34")
NA
round_hms()函数,是把时间进行四舍五入对齐。
按秒,以5的倍数进行对齐,四舍五入
round_hms(as.hms("12:34:51"), 5)
12:34:50
round_hms(as.hms("12:34:54"), 5)
12:34:55
round_hms(as.hms("12:34:56"), 5)
12:34:55
round_hms(as.hms("12:34:59"), 5)
12:35:00
按秒,以60的倍数对齐
round_hms(as.hms("12:34:56"), 60)
12:35:00
trunc_hms()函数,是把时间进行裁剪对齐。
按秒去掉末位,以5的倍数进行对齐
trunc_hms(as.hms("12:34:01"), 5)
12:34:00
trunc_hms(as.hms("12:34:44"), 5)
12:34:40
trunc_hms(as.hms("12:34:56"), 60)
12:34:00
生成时间序列
seq(as.Date("2015-02-01"), length=19, by="month")
[1] "2015-02-01" "2015-03-01" "2015-04-01" "2015-05-01" "2015-06-01" "2015-07-01"
[7] "2015-08-01" "2015-09-01" "2015-10-01" "2015-11-01" "2015-12-01" "2016-01-01"
[13] "2016-02-01" "2016-03-01" "2016-04-01" "2016-05-01" "2016-06-01" "2016-07-01"
[19] "2016-08-01"
可以参考:
https://mp.weixin.qq.com/s/epJLOp0iT9ISPQjDXqNM7g