gpt4 book ai didi

r - 如何根据 R 中列中的值有条件地执行许多 Mann-Whitney 检验?

转载 作者:行者123 更新时间:2023-12-04 10:41:58 26 4
gpt4 key购买 nike

我想创建一个执行 Mann Whitney 测试的函数(或一种一次性执行测试的方法)。我想在同一时间内分析 2 个不同 CC 条件之间的 logSG 值。因此,对于以下数据帧,我想要 3 个对应于每个时间的 p 值。

我的示例数据框:

structure(list(Time = c("30", "30", "30", "30", "30", "30", "30", 
"30", "30", "30", "30", "30", "30", "60", "60", "60", "60", "60",
"60", "60", "60", "60", "60", "90", "90", "90", "90", "90", "90",
"90", "90", "90"), CC = c("Scramble", "Scramble", "Scramble",
"Scramble", "Scramble", "Scramble", "Scramble", "Scramble", "KD",
"KD", "KD", "KD", "KD", "Scramble", "Scramble", "Scramble", "Scramble",
"Scramble", "KD", "KD", "KD", "KD", "KD", "Scramble", "Scramble",
"Scramble", "Scramble", "KD", "KD", "KD", "KD", "KD"), logSG = c(0,
6.29469069760774, 6.97548510669835, 0, 0, 5.6529880324294, 0,
0, 0, 0, 0, 5.84818081635987, 0, 6.33960454566506, 0.410736902037262,
0, 0, 0, 0, 0.0294484401648161, 0, 1.03061195077248, -1.30321174424293,
-1.25902114646857, 0, 0, 0.787059500696643, 3.54611686297603,
0, 0, -0.297732408305282, 0)), row.names = c(NA, -32L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x7f9b120204e0>)

我尝试对每个时间点进行以下操作:
e <- result[result$Time == 30,]
wilcox.test(SG ~ CC, data=e)

这是笨拙且低效的。

或者,我无法使其正常工作:
t <- result %>% group_by(Time) %>% do(te=wilcox.test(logSG ~ CC))

如果可能的话,我想学习如何使用 dplyrm/s/apply 来做到这一点。

引用文献: link Link

最佳答案

如果我们使用 do ,然后指定 data

library(dplyr)
result %>%
group_by(Time) %>%
do(te=wilcox.test(logSG ~ CC, data = .))

或使用 mapnest数据集
library(purrr)
result %>%
group_by(Time) %>%
nest %>%
mutate(te = map(data, ~ wilcox.test(logSG ~ CC, data = .x) ))

关于r - 如何根据 R 中列中的值有条件地执行许多 Mann-Whitney 检验?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59884876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com