我有一张表格,包含两列"开始"和"结束",分别包含相应的开始和结束时间和日期,如下:
Sr. No. Start End
1 22May2001:00:00:00 27May2001:23:59:59
2 28May2001:00:00:00 26Jun2001:23:59:59
我希望将上述日期时间转换为以下格式(带有时间戳的ISO8601):
Sr. No. Start End
1 2001-05-22 00:00:00 2001-05-27 23:59:59
2 2001-05-28 00:00:00 2001-06-26 23:59:59
我使用了这个链接提供的代码:http://www.stat.berkeley.edu/~s133/dates.html。
View(my_table)
str(my_table)
my_table$startD <- as.Date(my_table$start, "%d%b%Y:%H:%M:%S")
my_table$startT <- strptime(my_table$start, format = "%d%b%Y:%H:%M:%S")
到目前为止,我的尝试给我带来了这样两列:
StartD StartT
2001-05-22 2001-05-22
这不是理想的。请问是否有人可以建议我通过以上或任何其他替代方法将日期时间转换为所需格式?
as.POSIXct
或strptime
进行解析,例如library(dplyr) ; df %>% mutate_at(-1, as.POSIXct, format = '%d%b%Y:%H:%M:%S')
或等效的方法。请注意,午夜时分的打印会省略时间部分,但仍然存储在其中。 - alistaireformat =“%d%b%Y:%H:%M:%S”
,因为否则它会认为解析字符串是第二个参数,用于设置时区。 - alistaire