Question

I'm trying to create a bar plot in R with breaks based on the hour of a datetime. Here's a sample of the data I'm working with:

    966,34,2013-12-27 06:48:20,219.0,921.423404575176661
    966,26,2013-12-23 07:19:18,147.0,1997.267748145552894
    966,18,2013-12-20 07:32:29,195.0,1569.149919735630304
    966,11,2013-12-19 12:15:19,955.0,4780.830543328801945

The first two columns are forms of ids which I am not interested in. The third column is the datetime object. The fourth and fifth columns are the ones I want to represent in bar plots (a plot for each column). My problem is that I cannot figure out how to calculate the average values of column 4 (and 5) based only on the hour of the day, and display that in a barplot.

Any help would be appreciated!

Was it helpful?

Solution

I would try something like this:

group <- list(strftime(df$datetime, format = "%H"))

to convert the datetime strings into a list of hours to aggregate by.

I'd then aggregate the column of values whose means you want:

means <- aggregate(df$values, by = group, FUN = mean)

I don't know whether this a particularly good method but it works for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top