POSIX formatted Date and Time

前端 未结 1 1941
甜味超标
甜味超标 2021-01-26 10:43

I am reading this from csv file into R

df <-

ID   DATE         TIME
1    10/14/2000    8:30:05
2    02/13/2001    12:05:05

I have t

1条回答
  •  长发绾君心
    2021-01-26 10:55

    The format argument needs to be the format of what it's reading, not what you want the output to be.

    Also, I assume your date component is in the American version of 'mm/dd/yyyy'

    Consider

    DateTime <- "10/14/2000 8:30:05"
    
    as.POSIXct(DateTime, format = "%m/%d/%Y %H:%M:%S")
    "2000-10-14 08:30:05 AEDT"
    

    So you'll want

    df$DateTimePOSIX <- as.POSIXct(df$DateTime,  format = "%m/%d/%Y %H:%M:%S")
    
    df
    #   ID       DATE     TIME            DateTime       DateTimePOSIX
    # 1  1 10/14/2000  8:30:05  10/14/2000 8:30:05 2000-10-14 08:30:05
    # 2  2 02/13/2001 12:05:05 02/13/2001 12:05:05 2001-02-13 12:05:05
    

    0 讨论(0)
提交回复
热议问题