首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环查找两个不同数据格式中相同变量(列)之间的相关性。

循环查找两个不同数据格式中相同变量(列)之间的相关性。
EN

Stack Overflow用户
提问于 2016-01-07 21:30:39
回答 2查看 205关注 0票数 2

我有两个数据文件: CNA和mrna_zscores。我试图使用这个for循环来查找两个数据文件中相同变量(列)之间的关联,但是它返回一个空向量(NULL).出什么问题了?

代码语言:javascript
复制
for(i in 1:50){
   cor_CNA_mRNA_kendall <- cor(CNA[, i], mrna_zscores[, i], method = "kendall")
}

我得到了我想要的结果,但是有很多代码,不太有用.

代码语言:javascript
复制
cor_CNA_mRNA_kendall <- c(cor(CNA[, 1], mrna_zscores[, 1], method = "kendall"), cor(CNA[, 2], mrna_zscores[, 2], method = "kendall"), cor(CNA[, 3], mrna_zscores[, 3], method = "kendall"), cor(CNA[, 4], mrna_zscores[, 4], method = "kendall"), cor(CNA[, 5], mrna_zscores[, 5], method = "kendall"), cor(CNA[, 6], mrna_zscores[, 6], method = "kendall"), cor(CNA[, 7], mrna_zscores[, 7], method = "kendall"), cor(CNA[, 8], mrna_zscores[, 8], method = "kendall"), cor(CNA[, 9], mrna_zscores[, 9], method = "kendall"), cor(CNA[, 10], mrna_zscores[, 10], method = "kendall"), cor(CNA[, 11], mrna_zscores[, 11], method = "kendall"), cor(CNA[, 12], mrna_zscores[, 12], method = "kendall"), cor(CNA[, 13], mrna_zscores[, 13], method = "kendall"), cor(CNA[, 14], mrna_zscores[, 14], method = "kendall"), cor(CNA[, 15], mrna_zscores[, 15], method = "kendall"), cor(CNA[, 16], mrna_zscores[, 16], method = "kendall"), cor(CNA[, 17], mrna_zscores[, 17], method = "kendall"), cor(CNA[, 18], mrna_zscores[, 18], method = "kendall"), cor(CNA[, 19], mrna_zscores[, 19], method = "kendall"), cor(CNA[, 20], mrna_zscores[, 20], method = "kendall"), cor(CNA[, 21], mrna_zscores[, 21], method = "kendall"), cor(CNA[, 22], mrna_zscores[, 22], method = "kendall"), cor(CNA[, 23], mrna_zscores[, 23], method = "kendall"), cor(CNA[, 24], mrna_zscores[, 24], method = "kendall"), cor(CNA[, 25], mrna_zscores[, 25], method = "kendall"), cor(CNA[, 26], mrna_zscores[, 26], method = "kendall"), cor(CNA[, 27], mrna_zscores[, 27], method = "kendall"), cor(CNA[, 28], mrna_zscores[, 28], method = "kendall"), cor(CNA[, 29], mrna_zscores[, 29], method = "kendall"), cor(CNA[, 30], mrna_zscores[, 30], method = "kendall"), cor(CNA[, 31], mrna_zscores[, 31], method = "kendall"), cor(CNA[, 32], mrna_zscores[, 32], method = "kendall"), cor(CNA[, 33], mrna_zscores[, 33], method = "kendall"), cor(CNA[, 34], mrna_zscores[, 34], method = "kendall"), cor(CNA[, 35], mrna_zscores[, 35], method = "kendall"), cor(CNA[, 36], mrna_zscores[, 36], method = "kendall"), cor(CNA[, 37], mrna_zscores[, 37], method = "kendall"), cor(CNA[, 38], mrna_zscores[, 38], method = "kendall"), cor(CNA[, 39], mrna_zscores[, 39], method = "kendall"), cor(CNA[, 40], mrna_zscores[, 40], method = "kendall"), cor(CNA[, 41], mrna_zscores[, 41], method = "kendall"), cor(CNA[, 42], mrna_zscores[, 42], method = "kendall"), cor(CNA[, 43], mrna_zscores[, 43], method = "kendall"), cor(CNA[, 44], mrna_zscores[, 44], method = "kendall"), cor(CNA[, 45], mrna_zscores[, 45], method = "kendall"), cor(CNA[, 46], mrna_zscores[, 46], method = "kendall"), cor(CNA[, 47], mrna_zscores[, 47], method = "kendall"), cor(CNA[, 48], mrna_zscores[, 48], method = "kendall"), cor(CNA[, 49], mrna_zscores[, 49], method = "kendall"), cor(CNA[, 50], mrna_zscores[, 50], method = "kendall"))

为什么在dataframe名称之后不使用没有括号的cor函数?好吧,它还给我这样的关联: V1xV1,V1xV2,V1xV3.V2xV1,V2xV2,V2xV3.V3xV1,V3xv2,V3xV3.

我只想要一个带有V1xV1,V2xV2,V3xV3的向量。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-07 21:42:23

如前所述,您需要创建一个变量来填充。

代码语言:javascript
复制
correlations<-data.frame()
for(i in 1:50){
   cor_CNA_mRNA_kendall <- cor(CNA[, i], mrna_zscores[, i], method = "kendall")
   correlations<-rbind(correlations,cor_CNA_mRNA_kendall)
}
票数 0
EN

Stack Overflow用户

发布于 2016-01-07 22:37:44

与R中通常的情况一样,最好的方法是不使用for循环。本质上,你所追求的是方差协方差矩阵的对角线。因此,您应该集中精力提取对角线。

使用R基

代码语言:javascript
复制
d1 <- matrix(sample(100), 10, 10)
d2 <- matrix(sample(100), 10, 10)

cor(d1, d2, method = "kendall")[as.logical(diag(nrow(d1)))]

使用OpenMx软件包

代码语言:javascript
复制
library(OpenMx)

diag2vec(cor(d1, d2, method = "kendall"))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34665527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档