Search code examples
rdatetimelubridate

Converting Charector to DateTime


I have a dataframe called sleepDay with a variable SleepDay such as below:

The dataframe

I tried converting sleepDay to dmy_hms using lubridate but some values are returning NA.

sleepDay <- mutate(sleepDay, ActivityDate = dmy_hms(SleepDay))

Can someone tell me why?


Solution

  • You haven't shared your actual data nor the image clearly shows the data. So I don't know if you have only dates in the data or both date and time.

    Nonetheless, the format in your data is Month/Date/Year so use mdy_hms if you have both date and time in the data or mdy if you have only dates.

    library(dplyr)
    library(lubridate)
    
    sleepDay <- mutate(sleepDay, ActivityDate = mdy_hms(SleepDay))