将Lubridate Period类时间戳格式转换为R中的时间戳格式
我的代码读取一个.txt文件,该文件在一列中包含一系列时间戳。我需要考虑专栏的夏令时,所以我使用lubridate软件包从这些时间戳中减去一个小时。我正在努力将周期类从lubridate转换回%I:%m%:S%p的时间格式 这是我的密码将Lubridate Period类时间戳格式转换为R中的时间戳格式,r,time,format,R,Time,Format,我的代码读取一个.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'), '%
# 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)
我希望得到像这样的输出
1:41:54 PM, 1:42:40 PM
有什么建议吗?谢谢大家! 如果我们需要减去一个小时,在原始datetime对象上执行此操作,然后执行
格式
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")
库(lubridate)
#//转换为Datetime类
原始数据样本$Time如果我们需要减去一个小时,在原始日期时间对象上执行此操作,然后执行格式
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")
库(lubridate)
#//转换为Datetime类
原始数据样本$Time您可以使用解析日期时间
函数将时段对象转换为POSIXct,然后使用格式
获得合适的格式
library(lubridate)
raw_data_sample$Time1 <- format(parse_date_time(raw_data_sample$Time, 'HMS'), '%I:%M:%S %p')
库(lubridate)
原始数据样本$Time1您可以使用parse\u date\u time
函数将时段对象转换为POSIXct,然后使用格式
获得合适的格式
library(lubridate)
raw_data_sample$Time1 <- format(parse_date_time(raw_data_sample$Time, 'HMS'), '%I:%M:%S %p')
库(lubridate)
原始数据样本$Time1感谢您的回复!我应该澄清一下,我最初的数据帧时间格式只是HH:MM:SS,没有日期。当我写你的建议时,我得到了“as.POSIXlt.character(x,tz,…)中的错误:字符串不是标准的明确格式”,我假设这是由于格式中缺少日期造成的。@user14880462只需在as.POSIXct
中指定格式,即格式=“%H:%m:%S”
哦,非常感谢!这解决了我的问题。谢谢你的回复!我应该澄清一下,我最初的数据帧时间格式只是HH:MM:SS,没有日期。当我写你的建议时,我得到了“as.POSIXlt.character(x,tz,…)中的错误:字符串不是标准的明确格式”,我假设这是由于格式中缺少日期造成的。@user14880462只需在as.POSIXct
中指定格式,即格式=“%H:%m:%S”
哦,非常感谢!这解决了我的问题。