我有两个数据帧:CNA和mrna_zscores。我试图使用这个for循环来查找两个数据帧中相同变量(列)之间的相关性,但它返回一个空向量(NULL)......出了什么问题?
for(i in 1:50){
cor_CNA_mRNA_kendall <- cor(CNA[, i], mrna_zscores[, i], method = "kendall")
}
我得到了我想要的结果但是有很多代码,不是很有用......
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"))
为什么我没有在数据框名称后没有括号的cor函数?好吧,它返回给我这样的相关性:V1xV1,V1xV2,V1xV3 ...... V2xV1,V2xV2,V2xV3 ...... V3xV1,V3xv2,V3xV3 ......
我想要的只是一个带有V1xV1,V2xV2,V3xV3的载体。
答案 0 :(得分:0)
如上所述,您需要创建一个填充变量。
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)
}
答案 1 :(得分:0)
与R中的情况一样,最好的方法是不使用for循环。你基本上是你的方差 - 协方差矩阵的对角线。因此,您应该集中精力提取对角线。
(function(){
setTimeout(function(){
$('.row').on('shown.bs.collapse', function(){
$(this).find(".glyphicon-plus").removeClass("glyphicon-plus").addClass("glyphicon-minus");
});
$('.row').on('hidden.bs.collapse', function(){
$(this).find(".glyphicon-minus").removeClass("glyphicon-minus").addClass("glyphicon-plus");
});
}, 1000);
});
d1 <- matrix(sample(100), 10, 10)
d2 <- matrix(sample(100), 10, 10)
cor(d1, d2, method = "kendall")[as.logical(diag(nrow(d1)))]