R - Help in Converting factor to date (%m/%d/%Y %H:%M)

前端 未结 4 1143
南旧
南旧 2021-01-23 20:57

I am importing a data frame into R, but R is not recognizing the columns with the dates as being in dates format.

> mydata[1,1] [1] 1/1/2003 0:00 216332 Levels:

4条回答
  •  故里飘歌
    2021-01-23 21:33

    The levels mean you have a factor. You need to convert to character with as.character():

     dt <- as.POSIXct(as.character(mydata[ ,1]) format = "%m/%d/%Y %H:%M")
    

    The first item with time = 0:00 will not show the time when printed but the others will. The error is occuring because the POSIXlt object is a list of 11 item lists. Generally it is better to use as.POSIXct than to use strptime because strptime returns a POSIXlt object and they are a bit of a mess to work with.:

    d <- factor("1/1/2003 0:01")
    as.POSIXct( as.character(d), format = "%m/%d/%Y %H:%M")
    [1] "2003-01-01 00:01:00 PST"
    

提交回复
热议问题