如何获得p值列表?

时间:2013-03-26 14:40:49

标签: r

我执行了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 

2 个答案:

答案 0 :(得分:7)

只需添加$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

答案 1 :(得分:2)

使用sapply并引用p.value名称...

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

#[1] 0.8247781 0.0765225 0.8247781 1.0000000 0.2682859 0.6625206 1.0000000
#[8] 0.1211833 0.5065552 0.8247781

这将返回一个向量而不是一个列表(我通常会发现向量更容易处理,例如因为你不能使用mylist[[1:5]]索引列表,但你可以做myvector[1:5]但是这是个人偏好