R: Average by every N value in 3-dim matrix -
i have matrix hourly data (on monthly period) , dim [116 152 744] trying create matrix b daily data , dim [116 152 31] every dim tstep in b average of first 24 tsteps in matrix a.
i successful in creating matrix c monthly data simple apply
c <- apply(a, c(1,2), function (x) mean(x))
but can't quite figure out average on every n values. thanks.
take 1 vector only, mean every 24 values, can do:
mean24 <- function(x) { dim(x) <- c(24, length(x) / 24) colmeans(x) } x <- 1:48 mean24(x) [1] 12.5 36.5
so, in case, have do:
apply(a, c(1, 2), mean24)
Comments
Post a Comment