Bonono Bonono - 1 year ago 53
R Question

Looping correlation tests within nested lists on same variables across more than two dataframes

Consider these three dataframes in a nested list:

df1 <- data.frame(a = runif(10,1,10), b = runif(10,1,10), c = runif(10,1,10))

df2 <- data.frame(a = runif(10,1,10), b = runif(10,1,10), c = runif(10,1,10))

df3 <- data.frame(a = runif(10,1,10), b = runif(10,1,10), c = runif(10,1,10))
dflist1 <- list(df1,df2,df3)
dflist2 <- list(df1,df2,df3)
nest_list <- list(dflist1, dflist2)

I want to do a 'cor.test' between column 'a' against column 'a', 'b' against 'b' and 'c' against 'c' in all 'dfs' for each
. I can do it individually if
each one to the global environment with the code below thanks to this post:

for (i in 1:length(nest_list)) { # extract dataframes from list in to individual dfs
for(j in 1:length(dflist1)) {

temp_df <- Norm_red_list[[i]][[j]]}

ds <- paste (names(nest_list[i]),names(nestlist[[i]][[j]]), sep = "_")



combn(paste0("df", 1:3), 2, FUN = function(x) { #a ctual cor.test
x1 <- mget(x, envir = .GlobalEnv)
Map(function(x,y) cor.test(x,y, method = "spearman")$p.value, x1[[1]], x1[[2]])})

Answer Source

I am not sure that I understand exactly what you want to do but could something like this help you ?

columns <- c("a","b","c")
n <- length(columns)
correl <- function(i,j,data) {cor.test(unlist(data[i]),unlist(data[j]), method = "spearman")$p.value}
correlfun <- Vectorize(correl, vectorize.args=list("i","j"))
res <- sapply(columns,function(u){
    lapply(lapply(nest_list,function(x){sapply(x,function(x){x[which(names(x)==u)]})}),function(z){outer(1:n,1:n,correlfun,data=z)})},simplify = FALSE,USE.NAMES = TRUE)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download