将char转换为日期时间

cco*_*ell 6 r

在data.frame中,我在表单中有一个日期时间戳:

head(x$time)
[1] "Thu Oct 11 22:18:02 2012" "Thu Oct 11 22:50:15 2012" "Thu Oct 11 22:54:17 2012"
[4] "Thu Oct 11 22:43:13 2012" "Thu Oct 11 22:41:18 2012" "Thu Oct 11 22:15:19 2012"
Run Code Online (Sandbox Code Playgroud)

每当我尝试将它转换为as.Date,lubridate动物园时,我都会得到NAs或错误.

将此时间转换为可读形式的方法是什么?

我试过了:

 Time<-strptime(x$time,format="&m/%d/%Y  %H:$M")
    x$minute<-parse_date_time(x$time)
    x$minute<-mdy(x$time)
    x$minute<-as.Date(x$time,"%m/%d/%Y %H:%M:%S")
    x$minute<-as.time(x$time)
    x$minute<-as.POSIXct(x$time,format="%H:%M")
    x$minute<-minute(x$time)
Run Code Online (Sandbox Code Playgroud)

A5C*_*2T1 18

你真正想要的是什么strptime().尝试类似的东西:

strptime(x$time, "%a %b %d %H:%M:%S %Y")
Run Code Online (Sandbox Code Playgroud)

作为您可以使用的有趣事项的示例strptime(),请考虑以下事项:

thedate <- "I came to your house at 11:45 on January 21, 2012."
strptime(thedate, "I came to your house at %H:%M on %B %d, %Y.")
# [1] "2012-01-21 11:45:00"
Run Code Online (Sandbox Code Playgroud)