gpt4 book ai didi

r - 如何获取 p 值列表?

转载 作者:行者123 更新时间:2023-12-02 15:32:06 26 4
gpt4 key购买 nike

我执行了 wilcox 测试,现在我想将 p.value 提取到列表或矩阵中。

DF <- data.frame(A1=sample(1:9, 10, T),
A2=sample(1:9, 10, T),
A3=sample(1:9, 10, T),
B1=sample(1:9, 10, T),
B2=sample(1:9, 10, T),
B3=sample(1:9, 10, T))


sampA <- DF[,grep('A', names(DF))] # Sample with columns A
sampB <- DF[,grep('B', names(DF))] # Sample with columns B


lapply(1:nrow(DF), function(i){
wilcox.test(as.numeric(sampA[i,]), as.numeric(sampB[i,]), exact=FALSE )
})

我对每行进行 wilcox 测试的结果如下所示:我想知道如何获取列表或矩阵中的 p.value 并导出到 Excel 文件? [[1]]

    Wilcoxon rank sum test with continuity correction

data: as.numeric(sampA[i, ]) and as.numeric(sampB[i, ])
W = 3, p-value = 0.6579
alternative hypothesis: true location shift is not equal to 0


[[2]]

Wilcoxon rank sum test with continuity correction

data: as.numeric(sampA[i, ]) and as.numeric(sampB[i, ])
W = 0, p-value = 0.0722
alternative hypothesis: true location shift is not equal to 0


[[3]]

Wilcoxon rank sum test with continuity correction

data: as.numeric(sampA[i, ]) and as.numeric(sampB[i, ])
W = 6, p-value = 0.6579
alternative hypothesis: true location shift is not equal to 0

最佳答案

只需添加 $p.value 即可从 wilcox.test 对象中提取 p.value :

lapply(1:nrow(DF), function(i){
wilcox.test(as.numeric(sampA[i,]), as.numeric(sampB[i,]), exact=FALSE )$p.value
})

这给出:

[[1]]
[1] 1

[[2]]
[1] 1

[[3]]
[1] 0.8247781

[[4]]
[1] 0.8247781

通过使用 sapply 而不是 lapply,您将获得一个向量而不是列表,这可能更容易操作。

sapply(1:nrow(DF), function(i){
wilcox.test(as.numeric(sampA[i,]), as.numeric(sampB[i,]), exact=FALSE )$p.value
})

# [1] 1.0000000 1.0000000 0.8247781 0.8247781 0.0765225 0.8247781 1.0000000
# [8] 0.8247781 0.2682859 0.0765225

关于r - 如何获取 p 值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15639855/

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