I'm having trouble calculating the average of a variable "count" for every hour over a few days. I have a dataset called data which looks like this:
Time count
1 2019-06-30 05:00:00 17
2 2019-06-30 06:00:00 18
3 2019-06-30 07:00:00 26
4 2019-06-30 08:00:00 15
5 2019-07-01 00:00:00 13
6 2019-07-01 01:00:00 23
7 2019-07-01 02:00:00 13
8 2019-07-01 03:00:00 22
It contains values for every hour for a few days. Now i want to calculate a value for each hour which is the average value for that hour over all the days. Something like this:
Time count
1 00:00 22
2 01:00 13
3 02:00 11
4 03:00 9
I'm new to R and only came as far as calculating the daily average:
DF2 <- data.frame(data, Day = as.Date(format(data$Time)))
aggregate(cbind(count) ~ Day, DF2, mean)
Time count
1 2019-06-30 22
2 2019-07-01 13
3 2019-07-02 11
4 2019-07-03 9
But i can't get it working with the hourly average. I tried to find a solution in other posts, but they eiher didn't work or seem to require a lot of unique calculations. There must be a simple way to do this in R.
Here is the output of dput(droplevels(head(data, 4))):
structure(list(Time = structure(1:4, .Label = c("2019-06-30 05:00:00",
"2019-06-30 06:00:00", "2019-06-30 07:00:00", "2019-06-30 08:00:00"
), class = "factor"), count = c(17L, 18L, 26L, 15L)), row.names = c(NA,
4L), class = "data.frame")
Any suggestion? Thank you in advance!
Maxi