Search code examples
rdatetimedate-histogram

Histogram of time only (not accounting the date)?


I have 181 Observations at definite time points. The times are a character variable in the format "yyyy-mm-dd hh:mm:ss".

I read them into a data frame via DF<-read.csv("myCSV.csv")

I managed do create a histogram of the days using the date string "yyyy-mm-dd" only (it doesn't take into account the time, though...).

I also need to do a histogram of the times. Unfortunately, I just don't know why it doesn't work.

mydate <- as.Date(DF$DateTime,format = "%Y-%m-%d %H:%M:%S")

will strip off the times. When doing

mytime <- substr(as.character(DF$DateTime),12,16)

It gives Errors when entering hist().

I want to see the distribution across the 24 hours of a day.

dput(head(ExampleDF))
structure(list(Date = c("2019-05-26", "2019-05-27", "2019-09-19", 
"2019-09-20", "2019-12-25", "2019-12-25"), Time = c("11:11:00", 
"04:00:00", "07:00:00", "11:11:00", "04:00:00", "11:00:00"), 
    DateTime = c("2019-05-26 11:11:00", "2019-05-27 04:00:00", 
    "2019-09-19 07:00:00", "2019-09-20 11:11:00", "2019-12-25 04:00:00", 
    "2019-12-25 11:00:00"), TimeOnly = c("1905-06-21 11:11:00", 
    "1905-06-21 04:00:00", "1905-06-21 07:00:00", "1905-06-21 11:11:00", 
    "1905-06-21 04:00:00", "1905-06-21 11:00:00"), Intensity = c(5L, 
    10L, 10L, 10L, 7L, 10L), Comments = c("Feeling ill for days", 
    "", "", "", "", ""), SeriesComments = c("This section covers some ", 
    "", "", "", "", "")), row.names = c(NA, 6L), class = "data.frame")

Solution

  • You can use lubridate::as_datetime() to transform your DF$DateTime to a date time and then make a histogram the hours using lubridate::hour():

    DF$DateTime <- lubridate::as_datetime(DF$DateTime)
    
    hist(lubridate::hour(DF$DateTime))
    

    enter image description here

    But with these data you may want to use a barplot instead of a histogram, which is simply:

    barplot(table(lubridate::hour(DF$DateTime)))
    

    enter image description here

    For thoroughness's sake, you can also use lubridate::day() and plot them both:

    par(mfrow = c(1,2))
    barplot(table(lubridate::day(DF$DateTime)), xlab = "Day of Month", col = "darkred")
    barplot(table(lubridate::hour(DF$DateTime)), xlab = "Hour of Day", col = "gold")
    

    enter image description here

    Data

    DF <- data.frame(DateTime = as.character(sample(seq(from = as.POSIXct("2023-04-10 00:00:00", tz = "UTC"), 
                                                        to =  as.POSIXct("2023-04-30 00:00:00", tz = "UTC"), 
                                                        by = "hour"), 181)))