我有以下时间戳:"03-APR-06 12.41.00.000000000 PM US/CENTRAL"
并需要将其转换为R兼容的时间戳。
我试过了:
structure(df2$ACTION_IN_DTTM_TZ,class=c('POSIXct'))
parse_date_time(df2$ACTION_IN_DTTM_TZ, "abdHMSzY")
strptime(df2$ACTION_IN_DTTM_TZ,format='%d/%b/%Y:%H:%M:%S:%p:%Z')
并且所有这些都产生了NA
structure(df2$ACTION_IN_DTTM_TZ,class=c('POSIXct'))
parse_date_time(df2$ACTION_IN_DTTM_TZ, "abdHMSzY")
strptime(df2$ACTION_IN_DTTM_TZ,format='%d/%b/%Y:%H:%M:%S:%p:%Z')
我想这个:2012-08-10 04:42:47
任何建议都非常感谢!谢谢!
我做了类似的事情:
a <- "03-APR-06 12.41.00.000000000 PM US/CENTRAL"
b <- (substr(a,1,18))
c <- as.POSIXct(b,format='%d-%b-%y %H.%M.%S')
包lubridate
为此提供了功能。我用一个简单的str_extract命令解压缩了时区。
library(lubridate)
library(stringr)
timestamp <- "03-APR-06 12.41.00.000000000 PM US/CENTRAL"
as_datetime(timestamp,tz=str_extract(timestamp,"\\S*$"))
[1] "2003-04-06 00:41:00 CST"
#without lubridate
strptime(strsplit(timestamp," \\S*$")[[1]][1],format="%y-%b-%d %I.%M.%S.%OS %p",tz=str_extract(timestamp,"\\S*$"))