2

I previously asked a question: (Calculate Means and Covariances for large list of dataframes, replacing loops with lapply). To apply a function to all dataframes in list Ccols , I defined a function consisting of two nested loops, and then fed it to lapply as follows:

Power_f<- function(X){

list1<- list()
for (index in 2:ncol(X)){

     list2<-list()
     for (i in 1:length(X[,index]) ){
          Data<- get(X[i,index])
          list2[[i]] <-Data
     }

Data2<- transform(Reduce(merge, lapply(list2, function(x) data.frame(x, rn = row.names(x)))), row.names=rn, rn=NULL)
list1[[index]]<- (Data2)
}
return(list1)
}

lapply(seq(from=2,to=(length(Ccols))), function(i) Power_f(Ccols[[i]]))

But the Code is still taking too long to run. Is there anyway to replace all the for loops with lapply? I have checked similar question but all solutions are case-specific and am at a loss.

El_1988
  • 339
  • 3
  • 13
  • I think it can be done, but you have to give an example input data set and the example output of your function. I say this, because when I use a nested lapply with my (random) list of data.frames I get an error. – lampros Oct 12 '17 at 18:47
  • Hi Lampros, I have been able to do it (still taking quite a while but significantly less) and will be adding my solution to this thread ASAP. it involves defining each lapply in a function consecutivley – El_1988 Oct 13 '17 at 13:51
  • Could you post your solution as an answer, as to possibly help other people facing the same problem. – RJJoling May 16 '18 at 15:32
  • @lampros my dear friend I have given code to created a sample data , please refer the above . I have commented please refer. – Harvey Jan 23 '19 at 09:11
  • @El_1988 do we have other option. somewhere I have read if we use library named parallel then such lapply would work efficiently. I will share the screen shot. I am working on optimization please share any solution if you get thank you – Harvey Jan 23 '19 at 09:14
  • @Harvey, what do you mean you've 'given code to create sample data'. I don't see any code in El_1988's question that creates sample data so that someone can use it in order to post a solution. – lampros Feb 08 '19 at 06:49

0 Answers0