Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get a list of pvalues?

Tags:

r

I performed a wilcox test and now I want to extract the p.value in to a list or matrix.

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 )
}) 

The result of my wilcox test for each rows looks like this: I wanna know how i can get the p.value in a list or a matrix to export to a excel file? [[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 
like image 898
Telma_7919 Avatar asked Mar 26 '13 14:03

Telma_7919


2 Answers

Just add $p.value to extract your p.value from your wilcox.test object :

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

Which gives :

[[1]]
[1] 1

[[2]]
[1] 1

[[3]]
[1] 0.8247781

[[4]]
[1] 0.8247781

By using sapply instead of lapply you would get a vector instead of a list, which could be easier to manipulate.

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
like image 187
juba Avatar answered Nov 02 '22 14:11

juba


Use sapply and reference the p.value name...

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

This will return a vector rather than a list (I usually find vectors to be easier to handle, e.g. because you can't index a list using mylist[[1:5]], but you can do myvector[1:5], but that's personal preference

like image 30
Simon O'Hanlon Avatar answered Nov 02 '22 12:11

Simon O'Hanlon