How to calculate maximum hourly average per day without using a loop in R

I have minute data for a day and I want to find the maximum hourly average. It doesn't have to be in an hour ( 5:07

~ 6:07

works too, which means I need to calculate the average for 00:00

~ 01:00

, 00:01

~ 01:01

, 00:02

~ 01:02

......). Anything I can use besides a loop?

+3


source to share


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







All Articles