当我使用strptime()剥离日期和时间时,它会将我的时间从类POSIXct转换为character。
> head(data)
timestamps value
1 2017-10-01 00:00:00 8.424
2 2017-10-01 01:00:00 7.832
3 2017-10-01 02:00:00 6.320
4 2017-10-01 03:00:00 6.696
5 2017-10-01 04:00:00 5.448
6 2017-10-01 05:00:00 4.992
> data$time <- format(data$timestamps,"%H:%M:%S")
> str(data)
'data.frame': 226 obs. of 3 variables:
$ timestamps: POSIXct, format: "2017-10-01 00:00:00" "2017-10-01 01:00:00" "2017-10-01 02:00:00" ...
$ value : num 8.42 7.83 6.32 6.7 5.45 ...
$ time : chr "00:00:00" "01:00:00" "02:00:00" "03:00:00" ...
您可以看到它将时间转换为character。如何用相同的POSIXct类剥离时间?
这个link here给出了如何将它转换成整数,并且只需要花费几个小时、几分钟的值。,我想要我的时间戳将它转换为Po660 it,而不是Integer。请阅读该链接中给出的行。如果您想对您的数据执行任何进一步的操作,我建议不要这样做。但是,如果您想绘制结果,那么这样做可能很方便。
这也不起作用:as.integer(format(Sys.time(), "%H%M%S"))
发布于 2017-10-11 11:14:06
唯一可能的解决方案是将字符时间转换为POSIXlt对象(该对象将附加当前日期)
> strptime('10:12:00',format='%H:%M:%S')
[1] "2017-10-11 10:12:00 IST"
> class(strptime('10:12:00',format='%H:%M:%S'))
[1] "POSIXlt" "POSIXt"
> 或者,lubridate单独提取小时,分钟,秒(作为整数),并与他们玩!
https://stackoverflow.com/questions/46686555
复制相似问题