【问题标题】:How to apply wilcox.test to compare every column of table如何应用 wilcox.test 比较表格的每一列
【发布时间】:2015-11-18 01:02:32
【问题描述】:

我在 r 中有一个名为 tbl 的数据表。 我想执行 wilcox 测试以将每一列与表中的每一列进行比较,并将 p 值存储在新表或矩阵中。

要比较一对列(在这种情况下为 1 和 2),我通常会这样做:

wilcox.test(tbl[,1],tbl[,2],alternative="t",paired=FALSE)

非常感谢任何帮助。

【问题讨论】:

  • 也许这篇较早的帖子可以帮助您。 link to post

标签: r


【解决方案1】:

一种方法是存储 tbl 的 2 路组合,然后使用 lapply 计算 p.values。像这样的:

#example data.frame
df <- data.frame(a = runif(10), b= runif(10), c=runif(10))

#calculate combinations of names
combinations <- as.data.frame(combn(names(df), 2), stringsAsFactors=FALSE)

#use the above combinations and calculate the wilcox.test to get the p.values
lapply(combinations, function(x) {
  wilcox.test(df[,x[1]] , df[, x[2]], alternative="t", paired=FALSE)$p.value
})

输出:

$V1
[1] 0.8534283

$V2
[1] 0.2175626

$V3
[1] 0.3526814

【讨论】:

  • 如果在创建组合data.frame时不指定stringsAsFactors=FALSE,名称将被保存为因子,稍后将仅将12提供给lapply函数导致每次都获得相同的 p.value。这很重要。
猜你喜欢
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多