3
votes

I have minute data for a day, and I want to find the max hourly average value. It doesn't have to be on the hour (5:07~6:07 works too, which means I have to calculate average value for 00:00~01:00, 00:01~01:01, 00:02~01:02......). Anything I can use other than loop?

1
What does your data look like? Sounds like a case for ?filter, that is, calculate a moving window average, then find the ?max: stackoverflow.com/questions/743812/…SimonG
use dput() on your data, then copy the output from the R console into your question, if you want more help.rbatt
It's a non-overlapping rolling mean, right? In that case, the problem is a bit simpler.rbatt
@rbatt No I need the overlapping rolling mean>< the link in the first comment contains several function that works for me. I should've attached a pic or a table to show my data (but I don't know how_(:з」∠)_ ).user5191542

1 Answers

1
votes

If x is a vector of times, 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 precise answer to your question,

> max.hourly.average
[1] 6.5