I am dealing with some large data in R:
I have a vector of normally distributed random numbers with length about 6400*50000, I need to sum every 4 elements in this vector to get a smaller one.
Is there any efficient way to do this in R?
My thoughts till now:
- using a matrix with ncol=10 and use apply function-- failed because the matrix size is too big;
- Try paralell and foreach package but no progress yet;
example code:
library(parallel)
library(RcppZiggurat)
library(doParallel)
library(foreach)
coreNums<-detectCores()
N1=6400
M=4
N2=N1/M
cl<-makeCluster(getOption("cl.cores", coreNums))
registerDoParallel(cl)
vector1<-zrnorm(N1*K)
vector2=foreach(i=1:(N2*K)) %dopar% {sum(vector1[M*(i-1)+1:M*i])}
vector2=unlist(vector)