在两个以上数据帧上的相同变量的嵌套列表中循环相关性测试

时间:2016-11-27 17:48:30

标签: r list loops correlation

在嵌套列表中考虑这三个数据帧:

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)

我想做一个&#39; cor.test&#39;在列&#39; a&#39;之间对于列&#39; a&#39;,&#39; b&#39;反对&#39; b&#39;和&#39; c&#39;反对&#39; c&#39;在所有&#d;&#39;对于每个dflist。由于this帖子,我可以通过下面的代码assign每个人单独执行此操作,感谢{{3}}帖子:

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

1 个答案:

答案 0 :(得分:1)

我不确定我是否完全理解你想要做什么,但是这样的事可以帮到你吗?

    #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)

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