【发布时间】:2021-06-25 01:12:15
【问题描述】:
我在数据框中的列之间运行了一些相关性计算,如下所示:
cor(df$length, df$col1, method = c("pearson"), use = "complete.obs")
cor(df$length, df$col1, method = c("spearman"), use = "complete.obs")
cor(df$length, df$col2, method = c("pearson"), use = "complete.obs")
cor(df$length, df$col2, method = c("spearman"), use = "complete.obs")
cor(df$length, df$col3, method = c("pearson"), use = "complete.obs")
cor(df$length, df$col3, method = c("spearman"), use = "complete.obs")
我正在试图弄清楚如何将这些结果放入他们自己的表格中,给出如下表格:
Col Pearsons Spearman
Col1 0.1 0.2
Col2 0.003 0.5
Col3 0.6 0.9
我一直在尝试更改类似问题的代码:
result <- do.call(rbind, by(df, df$length, FUN = function(x) {
tmp <- cor.test(x$Col1, x$length, method = "spearman")
}))
但这看起来不正确,我不确定如何将我的关联代码压缩到表格中 - 我可以使用哪些函数来处理我的关联代码?
输入数据示例:
df <- structure(list(length = c(144001L, 1731L, 337L), col1 = c(3L,
3L, 4L), col2 = c(8L, 2L, 6L), col3 = c(18L,
1L, 1L)), row.names = c(NA, -3L), class = c("data.table", "data.frame"
))
【问题讨论】:
标签: r dataframe correlation