Search code examples
rmeanlarge-data

Fast splitting of a vector and component wise mean


How can I write a fast function, that will

  • partitionate a dataframe in 4 parts of the same length w,x,y,z
  • return the per-index mean of w,x,y,z this mean should be 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?


Solution

  • Just found a hint about .rowMeans here. I guess this is a good solution.