Correlation/p values of all combinations of all rows of two matrices

后端 未结 4 1247
旧时难觅i
旧时难觅i 2020-12-20 04:20

I would like to calculate the correlation and the p value of that correlatio of each species (bac) to each of the factors (fac) in a second data frame. Both were measured at

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 04:46

    We can use expand.grid to get the combinations of rownames of 'bac' and 'fac', loop through the rows with apply specifying the MARGIN as 1, subset the rows of 'bac' and 'fac' based on the rownames, do the corr.test and extract the 'p' values as a list

    library(psych)
    do.call(c, apply(expand.grid(rownames(bac), rownames(fac)), 1, 
      function(x) list(corr.test(cbind(unlist(bac[1,]), unlist(fac[1,])))$p)))
    

提交回复
热议问题