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

后端 未结 3 1647
梦毁少年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条回答
  •  Happy的楠姐
    2020-12-03 08:47

    Simply you can use lubridate package which is super awesome and fast. for your purpose try this:

    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"))
    
    df$mix <- paste(df$Date, df$Time)
    df$mix <- parse_date_time(df$mix, 'Ymd HMS')
    

    Just you have to feed the correct format to it. I prefer it to as.POSICct because it is much more flexible and you have other functions to work with time variables.

提交回复
热议问题