0

I have a dataset which has a combined date-time column, which I would like to split into separate year, month, day and time columns. I usually use the lubridate library with appropriate arguments, but this particular column also has a character T in it too in each row.

How can I split this column by dropping the character T from each row of this column?

Date_Time
2020-01-01T00:48:00  
2020-01-01T00:46:00
2020-01-02T15:07:00
2020-01-02T15:07:00
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34

2 Answers2

3

You can use tidyr::separate -

tidyr::separate(df, Date_Time, c('Year', 'Month', 'Day', 'Time'), sep = '[T-]')

#  Year Month Day     Time
#1 2020    01  01 00:48:00
#2 2020    01  01 00:46:00
#3 2020    01  02 15:07:00
#4 2020    01  02 15:07:00

Or extract date and time after converting Date_Time to POSIXct type.

library(dplyr)
library(lubridate)


df %>%
  mutate(Date_Time  = ymd_hms(Date_Time), 
         Year = year(Date_Time), 
         Month = month(Date_Time), 
         Day = day(Date_Time),
         Time = format(Date_Time, '%T'))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

Base R solution:

cbind(
  df, 
  strcapture(
    pattern = "^(\\d{4})-(\\d{2})-(\\d{2})T(.*)$",
    x = df$Date_Time,
    proto = list(
      year = integer(), 
      month = integer(), 
      day = integer(),
      time = character()
    )
  )
)
hello_friend
  • 5,682
  • 1
  • 11
  • 15