Split date-time column into Date and time variables
How about
df$Date <- as.Date(df$Start)
df$Time <- format(df$Start,"%H:%M:%S")
df$Date <- as.Date(df$Start) # already got this one from the answers above
df$Time <- format(as.POSIXct(df$Start), format = "%H:%M:%S")
Use as.Date
to convert 'Start' to a variables of class Date
. For the time variable, we first convert 'Start' to POSIXct
. Then use format
to extract the time component as a string.
By seeing your column format, I'd say you could use as.POSIXct to properly format your column, and then use format() to extract the desired data.
This is the code I use when splitting a DateTime column,
df$Time <- format(as.POSIXct(df$Start,format="%Y:%m:%d %H:%M:%S"),"%H:%M:%S")
df$Date <- format(as.POSIXct(df$Start,format="%Y:%m:%d %H:%M:%S"),"%Y:%m:%d")