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?
3
votes
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
?filter
, that is, calculate a moving window average, then find the?max
: stackoverflow.com/questions/743812/… – SimonGdput()
on your data, then copy the output from the R console into your question, if you want more help. – rbatt