首页 文章

将因子转换为POSIXct

提问于
浏览
0

我有一个sql查询的日期作为一个因素,我想使它成为一个POSIXct

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

结果

> 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"

你可以看到“0001-03-20 LMT”不正确 . 你知道怎么把这个因子转换成POSIXct吗?

谢谢

2 回答

  • 3

    您可以使用 lubridate 使这更简单

    library(lubridate)
    
    # month-day-year hour-minute-second
    mdy_hms(f, tz="EST")
    [1] "2014-01-03 20:59:30 EST"
    
  • 0

    您只需要指定格式,因为您的日期/时间字符串是非常非标准甚至模糊的格式:

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

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

相关问题