我的代码读取一个.txt文件,该文件在一列中包含一系列时间戳。我需要考虑专栏的夏令时,因此我使用lubridate软件包从这些时间戳中减去一个小时。我正在努力将时间段类从lubridate转换回%I:%M%:S%p的时间格式。
这是我的代码。
# Changing from 24 Hr to 12 Hr Format #
raw_data_sample$Time <- format(strptime(raw_data_sample$Time, format='%H:%M:%S'), '%I:%M:%S %p')
# Subtracting an Hour for Daylight Savings
raw_data_sample$Time <- hms(raw_data_sample$Time)
raw_data_sample$Time <- raw_data_sample$Time - hours(1)
这是我当前的输出。
c("1H 41M 54S", "1H 42M 4S", "1H 42M 14S", "1H 42M 31S", "1H 42M 41S", "1H 43M 1S")
我希望得到这样的输出
1:41:54 PM, 1:42:40 PM
有什么建议吗?谢谢!
如果需要减去一个小时,请对原始的datetime对象执行此操作,然后执行format
ting
library(lubridate)
# // convert to Datetime class
raw_data_sample$Time <- as.POSIXct(raw_data_sample$Time, format = "%H:%M:%S")
# // subtract 1 hour from the Datetime and use format to change the format
format(raw_data_sample$Time %m-% hours(1), "%I:%M:%S %p")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句