两个矩阵的所有行的所有组合的相关度/p值 [英] Correlation/p values of all combinations of all rows of two matrices
问题描述
bac1 <- c(1,2,3,4,5)
bac2 <- c(2,3,4,5,1)
bac3 <- c(4,5,1,2,3)
bac4 <- c(5,1,2,3,4)
bac <- as.data.frame(cbind(bac1, bac2, bac3, bac4 ))
colnames(bac) <- c("station1", "station2", "station3", "station4")
rownames(bac) <- c("bac1", "bac2", "bac3", "bac4", "bac5")
fac1 <- c(1,2,3,4,5,6)
fac2 <- c(2,3,4,5,1,6)
fac3<- c(3,4,5,1,2,6)
fac4<- c(4,5,1,2,3, 6)
fac <- as.data.frame(cbind(fac1, fac2, fac3, fac4))
colnames(fac) <- c("station1", "station2", "station3", "station4")
rownames(fac) <- c("fac1", "fac2", "fac3", "fac4", "fac5", "fac6")
我想象结果看起来像这样,在某处保留名称以知道呈现哪种组合:
bac1-fac1 cor1 p1
bac1-fac2 cor2 p2
bac1-fac3 cor3 p3
bac2-fac1 corx px...
我看过Hmist的rcorr函数和psych的corr.test函数,但是找不到行进行必要排列的示例...有什么想法吗?
如果您重组数据,以便计算成对的列之间的相关性,那将非常容易.
tbac <- data.frame(t(bac))
tfac <- data.frame(t(fac))
f <- function (x, y) cor(x, y)
tab <- outer(tfac, tbac, Vectorize(f))
as.data.frame.table(tab)
我有一个使用相同想法的答案:匹配数据和计数相同值的数字.
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 the same number of stations, but the number of bac and fac don't match.
bac1 <- c(1,2,3,4,5)
bac2 <- c(2,3,4,5,1)
bac3 <- c(4,5,1,2,3)
bac4 <- c(5,1,2,3,4)
bac <- as.data.frame(cbind(bac1, bac2, bac3, bac4 ))
colnames(bac) <- c("station1", "station2", "station3", "station4")
rownames(bac) <- c("bac1", "bac2", "bac3", "bac4", "bac5")
fac1 <- c(1,2,3,4,5,6)
fac2 <- c(2,3,4,5,1,6)
fac3<- c(3,4,5,1,2,6)
fac4<- c(4,5,1,2,3, 6)
fac <- as.data.frame(cbind(fac1, fac2, fac3, fac4))
colnames(fac) <- c("station1", "station2", "station3", "station4")
rownames(fac) <- c("fac1", "fac2", "fac3", "fac4", "fac5", "fac6")
I imagine the result looking somewhat like this, somewhere keeping the names to know which combination is presented:
bac1-fac1 cor1 p1
bac1-fac2 cor2 p2
bac1-fac3 cor3 p3
bac2-fac1 corx px...
I have looked at function rcorr from Hmist and corr.test from psych, but can't find an example with the neccessary permutation of rows...Any ideas?
If you restructure your data, such that you compute correlation between paired columns, it would be super easy.
tbac <- data.frame(t(bac))
tfac <- data.frame(t(fac))
f <- function (x, y) cor(x, y)
tab <- outer(tfac, tbac, Vectorize(f))
as.data.frame.table(tab)
I had an answer using the same idea: Match data and count number of same value.
这篇关于两个矩阵的所有行的所有组合的相关度/p值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!