问题描述
当我使用 strptime()
删除日期和时间时,它将我的时间从类 POSIXct
转换为 character
.
When i strip date and time using strptime()
it converts my time from class POSIXct
to 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 ...
您可以看到它将时间转换为 character
.如何使用相同的 POSIXct
剥离时间?
You can see it converts time to character
. How do i strip time with same class POSIXct
?
此此处的链接提供了如何将其转换为Integer以及仅花费数小时,数分钟的价值.我希望我的时间戳将其转换为Posixct而不是Integer .另请阅读该链接中给出的行.如果您要对数据进行任何进一步的操作,我不建议这样做.但是,如果要绘制结果,这样做可能会很方便.
This link here gives how to convert it to Integer and take hours, minutes values alone. I want my timestamp to convert it to Posixct and Not Integer. Also read the line given in that link.I would not advise to do that if you want to do any further operations on your data. However, it might be convenient to do that if you want to plot the results.
这也不起作用: as.integer(format(Sys.time(),%H%M%S"))
推荐答案
唯一可行的解决方案是将字符时间转换为POSIXlt对象(它将当前日期附加到该对象上)
The only possible solution is to convert the character time into POSIXlt object (which would append current date to it)
> 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
分别提取小时,分钟,秒(作为整数)并与它们一起玩!
Alternatively, lubridate
to individually extract Hour, Min, Seconds (as integers) and playing around with them!
这篇关于R中的strptime()将时间戳记类从Posixct转换为字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!