我有一个像
这样的时间戳矢量time_stamp <- c("7/1/2013", "7/1/2013 12:00:30 AM", "7/1/2013 12:01:00 AM", "7/1/2013 12:01:30 AM", "8/1/2013","8/1/2013 11:02:30 PM")
我想将此格式化为日期类。我试过了
strptime(time_stamp, format = "%d/%m/%Y %H:%M:%S", tz = "GMT")
但由于两个时间戳丢失的次数导致NA
s,应默认替换为:00:00:00。
我可以运行一个循环,例如:
for (i in 1:length(time_stamp))
{
if(nchar(time_stamp[i])<11)
{
time_stamp[i] <- paste(time_stamp[i], " 12:00:00 AM")
}
}
time_stamp <- format(strptime(time_stamp, format = "%d/%m/%Y %I:%M:%S %p", tz = "GMT"), "%d/%m/%Y %H:%M:%S", tz = "GMT")
有没有更快更清洁的方法来实现这一目标?矢量是大型数据集的一部分,所以我不想循环它。
答案 0 :(得分:3)
lubridate::parse_date_time
可以接受多个令牌订单,无论是否有%
:
lubridate::parse_date_time(time_stamp, orders = c("dmy IMS p", "dmy"))
## [1] "2013-01-07 00:00:00 UTC" "2013-01-07 00:00:30 UTC" "2013-01-07 00:01:00 UTC"
## [4] "2013-01-07 00:01:30 UTC" "2013-01-08 00:00:00 UTC" "2013-01-08 23:02:30 UTC"
或使用其truncated
参数:
lubridate::parse_date_time(time_stamp, orders = 'dmy IMS p', truncated = 4)
返回同样的东西。
答案 1 :(得分:2)
或者使用一些正则表达式替换然后正常处理:
as.POSIXct(sub("(\\d{4}$)", "\\1 00:00:00", time_stamp),
format = "%d/%m/%Y %H:%M:%S", tz = "GMT")
#[1] "2013-01-07 00:00:00 GMT" "2013-01-07 12:00:30 GMT" "2013-01-07 12:01:00 GMT"
#[4] "2013-01-07 12:01:30 GMT" "2013-01-08 00:00:00 GMT" "2013-01-08 11:02:30 GMT"