How can I write a fast function, that will
m=(w+x+y+z)/4
(note that these are letters all vectors)Example data may look like this:
# my data + noise 4 times
a <- 1:1000 + rnorm(10)
b <- 1:1000 + rnorm(10)
c <- 1:1000 + rnorm(10)
d <- 1:1000 + rnorm(10)
mydf <- data.frame(time=1:4000, measurement=c(a,b,c,d))
Till now I use the following slow workaround. And apply the function on mydf$measurement
AvgOverPeriodsVector <- function(hdata, recordedperiods=4){
SamplesPerPeriod <- length(hdata)/recordedperiods
a <- unname(sapply((split(hdata, rep(1:SamplesPerPeriod,recordedperiods))),mean ))
return(a)
}
How can I improve the speed?
Would rowMean
like in
Element-wise mean in R
be faster?
Just found a hint about .rowMeans
here. I guess this is a good solution.