将因子转换为 POSIXct

use*_*875 1 r posixct

我有一个来自 sql 查询的日期作为一个因素,我想将其设为 POSIXct

f<- as.factor("01/03/2014 20:59:30")
f
class(f)
po<-as.POSIXct(f)
po
Run Code Online (Sandbox Code Playgroud)

结果

> f<- as.factor("01/03/2014 20:59:30")
> f
[1] 01/03/2014 20:59:30
Levels: 01/03/2014 20:59:30
> class(f)
[1] "factor"
> po<-as.POSIXct(f)
> po
[1] "0001-03-20 LMT"
Run Code Online (Sandbox Code Playgroud)

您可以看到“0001-03-20 LMT”不正确。你知道如何将此因子转换为 POSIXct 吗?

谢谢

bgo*_*dst 5

您只需要指定格式,因为您的日期/时间字符串采用非常不标准甚至不明确的格式:

r> as.POSIXct(f,format='%d/%m/%Y %H:%M:%S');
[1] "2014-03-01 20:59:30 EST"
Run Code Online (Sandbox Code Playgroud)

请参阅http://stat.ethz.ch/R-manual/R-devel/library/base/html/as.POSIXlt.html上的文档。