user1884763
user1884763

Reputation: 53

How to separate date and time example: 12/25/17 0:00 in R?

How do I separate date and time into 2 different variables if the_date column is as follows:

data set image for reference

the_date
12/25/17 0:00

How can I separately retrieve

Upvotes: 1

Views: 52

Answers (1)

akrun
akrun

Reputation: 887118

After converting to DateTime, then we extract each of the components

library(lubridate)
library(dplyr)
df1 %>%
    mutate(v1 = mdy_hm(v1), 
           Year = year(v1),
           Month = month(v1), 
           Date = day(v1), 
           time = format(v1, "%H:%M:%S"))
# A tibble: 1 x 5
#   v1                   Year Month  Date time    
#   <dttm>              <dbl> <dbl> <int> <chr>   
#1 2017-12-25 00:00:00  2017    12    25 00:00:00

data

df1 <- tibble(v1 = "12/25/17 0:00")

Upvotes: 1

Related Questions