Pardon me if this questions has been answered before but I searched and couldn't find one. I have a list containing multiple lists containing two dataframes. I want to apply t.test between first row of dataframe 1 and first row of dataframe 2 and so on.
I tried this:
list1 <- list(set1 = data.frame(rnorm(100), rexp(100)), set2 = data.frame(rnorm(100, mean = 5, sd = 3), rexp(100, rate = 4)))
list2 <- list(set1 = data.frame(rnorm(100), rexp(100)), set2 = data.frame(rnorm(100, mean = 6, sd = 4), rexp(100, rate = 2)))
mylist <- list(list1, list2)
ttest<-function(list){
df1 <- list$set1
df2 <- list$set2
testresults<-rep(NA,nrow(df1))
for (j in seq(nrow(df1))){
testresults[j] <- t.test(df1[j,], df2[j,])$p.value
}
return(as.matrix(testresults))}
lapply(mylist,ttest)
This works fine but takes a lot of time because of this for loop and since the actual data is much larger. I want to replace the for loop with an apply function(if possible). Please suggest.
t.testfunction, which you can verify by profiling your code. Your memory-allocation + for loop approach is actually correct here, and the fastest way to do it.