R - converting date and time fields to POSIXct with HHMMSS format

后端 未结 3 1650
梦毁少年i
梦毁少年i 2020-12-03 08:37

I have a data file which has three columns thus:

20010101 000000  0.833
20010101 000500  0.814
20010101 001000  0.794
20010101 001500  0.772
...
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 08:47

    You were very close. The following "simply" forces the first two columns to be read as character strings, which saves the leading zeros.

    R> df <- read.table(text="20010101 000000  0.833
    20010101 000500  0.814
    20010101 001000  0.794
    20010101 001500  0.772", 
    + header=FALSE, colClasses=c("character", "character", "numeric"), 
    + col.names=c("Date", "Time", "Val"))
    R> df
          Date   Time   Val
    1 20010101 000000 0.833
    2 20010101 000500 0.814
    3 20010101 001000 0.794
    4 20010101 001500 0.772
    

    Now what you were attempting "just works":

    R> df$DateTime <- as.POSIXct(paste(df$Date, df$Time), format="%Y%m%d %H%M%S")
    R> df
          Date   Time   Val            DateTime
    1 20010101 000000 0.833 2001-01-01 00:00:00
    2 20010101 000500 0.814 2001-01-01 00:05:00
    3 20010101 001000 0.794 2001-01-01 00:10:00
    4 20010101 001500 0.772 2001-01-01 00:15:00
    R> 
    

提交回复
热议问题