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

白昼怎懂夜的黑 提交于 2019-11-28 08:31:21

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> 

You just need to import the data as character:

txt <- "Date  Time  value
20010101 000000  0.833
20010101 000500  0.814
20010101 001000  0.794
20010101 001500  0.772
"

df <- read.table(text=txt, header=TRUE, 
                 colClasses=c("character", "character", "numeric"))

df$DateTime <- as.POSIXct(paste(df$Date, df$Time), format="%Y%m%d %H%M%S")

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!