How to calculate maximum hourly average per day without using a loop in R
1 answer
If x
is is a vector of time, as a character string, and y
is the data you want to average, you can do something like this:
x <- c("0:00", "0:01", "12:00", "12:05", "18:04", "18:05", "18:06", "18:07", "0:00", "0:01")
x <- gsub(":[0-9]{2,}", "", x ,perl=T)
y <- 1:length(x)
hourly.average <- aggregate(list(y=y), by=list(x=x), mean)
max.hourly.average <- max(hourly.average[,"y"])
So you get
> hourly.average
x y
1 0 5.5
2 12 3.5
3 18 6.5
And for the exact answer to your question
> max.hourly.average
[1] 6.5
+1
source to share