Can rbind be parallelized in R?

后端 未结 6 769
无人共我
无人共我 2020-12-01 03:14

As I am sitting here waiting for some R scripts to run...I was wondering... is there any way to parallelize rbind in R?

I sitting waiting for this call to complete

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 03:49

    Looks like this has already been well answered by a number of people, but if it comes up for some one, here is a version of a parallel rbind for non-data.table/data.frame-esque objects:

    rbind.parallel <- function(list,ncore)
      {
      library(parallel)
      do.call.rbind<-function(x){do.call(rbind,x)}
      cl<-makeCluster(ncore)
      list.split<-split(list,rep(1:ncore,length(list)+1)[1:length(list)])
      list.join<-parLapply(cl,list.split,do.call.rbind)
      stopCluster(cl)
      list.out<-do.call(rbind,list.join)
      return(list.out)
      }
    

    This works effectively on sf type objects. For example, if you read in a list of shapefiles from a directory using lapply(.,st_read), obviously rbind.fill and its variants not going to work to join all the features.

提交回复
热议问题