在两个以上数据框内的相同变量的嵌套列表内循环相关性测试 [英] Looping correlation tests within nested lists on same variables across more than two dataframes

查看:68
本文介绍了在两个以上数据框内的相同变量的嵌套列表内循环相关性测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在嵌套列表中考虑以下三个数据框:

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)

我想对每个dflist的所有'dfs'中的列'a'相对于列'a','b'相对于'b'和'c'相对于'c'进行'cor.test'.我可以分别使用

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 dflist. I can do it individually if assign 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 = "_")

assign(ds,temp_df)

  }
 }

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]])})

推荐答案

我不确定我是否完全了解您想要做什么,但是这样可以帮助您吗?

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

    #vector of your columns name
    columns <- c("a","b","c")
    n <- length(columns)
    # correlation calculation function
    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"))
    # Make a "loop" on columns vector (u will then be each value in columns vector, "a" then "b" then "c")
    res <- sapply(columns,function(u){
        # Create another loop on frames that respect the condition names(x)==u (only the data stored in columns "a", "b" or "c")
        lapply(lapply(nest_list,function(x){sapply(x,function(x){x[which(names(x)==u)]})}),function(z)
   # on those data, use the function outer to apply correlfun function on each pair of vectors
{outer(1:n,1:n,correlfun,data=z)})},simplify = FALSE,USE.NAMES = TRUE)

这有帮助吗?不确定我的解释是否很清楚:)

Is this helping ? Not sure I'm really clear in my explanation :)

这篇关于在两个以上数据框内的相同变量的嵌套列表内循环相关性测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆