Rui Rui - 1 month ago 12
R Question

Group a set of array files into ONE large array in R

I would like to merge a set of 100 independent array files, dim= ({x=684,y=483,t=3} (t=time)), into one large array with dim=(x=684,y=483,t=3,runs=100).

I have ran a serious of (100) simulation in a loop form.
To distinguish each simulation I did:

run.number<-as.integer(runif(1)*10^5) # sets a random number with a small likelihood of repeating itself within the 100 runs
hP=paste0(run.number,".P_dynamics",”.RData”)# file name
save(historyP,file=hP) # save array with name = “hP”


example of a saved files: 5468.P_dynamics.RData and 61952.P_dynamics.RData*

problem:
I have 100 independent array files like above, and I would like to group them into one array, as:

dynamics<-array(NA,c(x=684,y=483,t=3,runs=100))


How can I do this? Taking into account that the generated number “run.number” is a large random number. Additionally, the order of runs is very important. Hence older file has to come before the newer array.

Answer

abind::abind() combines multi-dimensional arrays, it is easy. The difficult point is preparation of arguments of abind().

(1) move all xxx.RData into a folder.

(2) get the filenames, change it into old-new order, and get the object names of array.

library(dplyr); library(abind)

fl <- list.files("FolderName", full = T)
fl <- fl[order(file.info(fl)$ctime)]
array_names <- sapply(fl, load) %>% as.vector()


  • When all array has a unique name;

(3) load all xxx.RData, make the list of arrays by lapply(), and combine it using abind::abind(list, rev.along = 0)

for(i in fl) load(i)
ar <- lapply(array_names, function(x) eval(parse(text = x))) %>% 
  abind(rev.along = 0)


  • When arrays have a common name (i.e., length(unique(array_names)) is 1);

(3) separately attach() and put into list()

uni_n <- unique(array_names)
array_list <- list()

for(i in 1:length(fl)) {
  attach(fl[i])
  array_list[[i]] <- eval(parse(text = uni_n))
  detach()
}

ar <- abind(array_list, rev.along = 0)