如何导入日期和时间变量。

时间:2014-12-15 14:15:22

标签: r

我有这样的数据集。

sd<-structure(list(DateTime = structure(c(5L, 4L, 1L, 2L, 6L, 7L, 3L), .Label = c("9/1/2014 8:00", 
"9/20/2014 14:06", "9/21/2014 15:57", "9/24/2014 14:13", "9/26/2014 15:00", 
"9/26/2014 9:04", "9/28/2014 14:02"), class = "factor"), User_ID = c(4.56789e+14, 
9.45678e+14, 3.45678e+14, 4.56789e+14, 4.56789e+14, 9.45678e+14, 
9.45678e+14)), .Names = c("DateTime", "User_ID"), class = "data.frame", row.names = c(NA, 
-7L))

当我尝试使用fread将此类数据导入R时,它将DateTime变量视为字符。我怎样才能克服这个问题......请帮助我..

1 个答案:

答案 0 :(得分:0)

一种方法是

library(lubridate)
mdy_hm(as.character(sd$DateTime))
# [1] "2014-09-26 15:00:00 UTC" "2014-09-24 14:13:00 UTC"
# [3] "2014-09-01 08:00:00 UTC" "2014-09-20 14:06:00 UTC"
# [5] "2014-09-26 09:04:00 UTC" "2014-09-28 14:02:00 UTC"
# [7] "2014-09-21 15:57:00 UTC"

修改

format(mdy_hm(as.character(sd$DateTime)), "%m-%d-%Y %H:%M:%S")