-1

I have column called trip_start_timestamp with values like "01/23/2020 03:00:00 PM" and column datatype is factor. I am looking to have the column values as "2020/01/23 15:00:00" and the weekday for the specific value like "thursday".

  • You should provide a reproducible example. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Check out `lubridate`: https://lubridate.tidyverse.org/ – william3031 Mar 01 '21 at 03:36

1 Answers1

1

You can use mdy_hms from lubridate to get data into POSIXct and use weekdays to get day of the week.

library(dplyr)
library(lubridate)

df %>%
  mutate(trip_start_timestamp = mdy_hms(trip_start_timestamp), 
         weekday = weekdays(trip_start_timestamp))

#  trip_start_timestamp  weekday
#1  2020-01-23 15:00:00 Thursday
#2  2020-01-25 01:00:00 Saturday

In base R :

df$trip_start_timestamp <- as.POSIXct(df$trip_start_timestamp, 
                                 format = '%m/%d/%Y %I:%M:%S %p', tz = 'UTC')
df$weekday <- weekdays(df$trip_start_timestamp)
df

data

df <- data.frame(trip_start_timestamp = factor(c("01/23/2020 03:00:00 PM", 
                                                 "01/25/2020 01:00:00 AM")))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213