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.