How to convert Chr to time?

Hi all,

I'm a self-taught R practitioner(beginner basically :sweat_smile:). I am creating my portfolio project and I got such with date/time conversion.

I have a data set in which the data is in Chr contains both the date and time together. I created two new columns for date and time and split the existing date column accordingly. The new columns are also created in CHR format. I am able to convert the data column into data format but not the time column. I tried multiple functions but they either give an error or show NA. I tried a new method but it's not accurate when I try to plot. Can someone please let me know how can I convert the time into the correct format?

intensity_per_hour_clean<- intensity_per_hour
intensity_per_hour_clean[c('Date', 'Time')] <- str_split_fixed(intensity_per_hour_clean$ActivityHour, " ",2)
intensity_per_hour_clean$Time<-  hms::as_hms(intensity_per_hour_clean$Time)

Plotting issue:

I want to show the average intensity throughout the day but cos of the wrong time formatting it's not plotting what it's supposed to be.

ggplot(data=intensity_per_hour_clean, aes(x= Time , y= AverageIntensity))+geom_bar(stat = "identity", fill="blue")+
  theme(axis.text.x = element_text(angle = 90))+
  labs(title = "Time of The Day vs. Avg. Intensities")

Please, anyone, let me know how to fix this, I have been stuck here for a couple of days now!!

For dates and times, use lubridate :

x <- c("2010-04-14-04-35-59", "2010-04-01-12-00-00")
[1] "2010-04-14 04:35:59 UTC" "2010-04-01 12:00:00 UTC"

see the docs for the other formats available

1 Like

Cool!! Thanks... in my case, it was mdy_hms(x) and now it works.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.