【发布时间】:2016-03-23 11:13:48
【问题描述】:
我正在尝试生成相关矩阵的表格输出。具体来说,我正在使用 for 循环来确定 4:40 列与第 1 列中的所有数据之间的相关性。虽然该表的结果不错,但它确实没有识别正在比较的内容到什么。在检查cor.test 的属性时,我发现data.name 被指定为x[1] 和y[1],这不足以追溯哪些列与哪些列进行了比较。这是我的代码:
input <- read.delim(file="InputData.txt", header=TRUE)
x<-input[,41, drop=FALSE]
y=input[,4:40]
corr.values <- vector("list", 37)
for (i in 1:length(y) ){
corr.values[[i]] <- cor.test(x[[1]], y[[i]], method="pearson")
}
lres <- sapply(corr.values, `[`, c("statistic","p.value","estimate","method", "data.name"))
lres<-t(lres)
write.table(lres, file="output.xls", sep="\t",row.names=TRUE)
输出文件如下所示:
statistic p.value estimate method data.name
1 -2.030111981 0.042938137 -0.095687495 Pearson's product-moment correlation x[[1]] and y[[i]]
2 -2.795786248 0.005400938 -0.131239287 Pearson's product-moment correlation x[[1]] and y[[i]]
3 -2.099114632 0.036368337 -0.098908573 Pearson's product-moment correlation x[[1]] and y[[i]]
4 -1.920649487 0.055413178 -0.090571599 Pearson's product-moment correlation x[[1]] and y[[i]]
5 -1.981326962 0.048168291 -0.093408365 Pearson's product-moment correlation x[[1]] and y[[i]]
6 -2.80390736 0.00526909 -0.131613912 Pearson's product-moment correlation x[[1]] and y[[i]]
7 -1.265138839 0.206482153 -0.059798855 Pearson's product-moment correlation x[[1]] and y[[i]]
8 -2.861448156 0.004415411 -0.134266636 Pearson's product-moment correlation x[[1]] and y[[i]]
9 -2.103403363 0.035990039 -0.099108672 Pearson's product-moment correlation x[[1]] and y[[i]]
10 -3.610094985 0.000340807 -0.168498786 Pearson's product-moment correlation x[[1]] and y[[i]]
显然,这并不完美,因为行是编号的,并且无法区分哪些相关性与哪些相关性。有没有办法来解决这个问题?我尝试了许多解决方案,但都没有奏效。我知道诀窍在于编辑 data.name 属性,但我不知道该怎么做。
【问题讨论】:
标签: r statistics correlation