S.R.
S.R.

Reputation: 25

p.value extraction from an apply-function

I would like to extract only the p-values from an apply-function. I did the several statistical tests (shapiro-test, kruskal-Vallis and ANOVA) over several columns (A-D). It works aut, but I always get a the whole list as results.

My data.frame is calles data1.

b<-apply(data1 [,c("A", "B","C","D")],2,shapiro.test);b

If I add $p.value or $p.val to the function the outcome shows "NULL" ("Zero").

b<-apply(data1 [,c("A", "B","C","D")],2,shapiro.test)$p.val

b$p.val NULL

Can anybody help me in this matter?

Upvotes: 0

Views: 2353

Answers (1)

spsaaibi
spsaaibi

Reputation: 452

S.R,

with apply you will obtain a list of the shapiro.test object. To extract the values from this list into a data.frame, you could:

b<-apply(data1[,c("A", "B","C","D")],2,shapiro.test)
do.call(rbind,lapply(b,function(v){v$p.value}))

This will give you a data.frame with one p.value per row.

Using mtcars to build a reproducible example:

b<-apply(mtcars[,c("disp","hp","drat","wt","qsec")],2,shapiro.test)
do.call(rbind,lapply(b,function(v){v$p.value}))

Which yields a data.frame of p.value values.

          [,1]
disp 0.02080657
hp   0.04880824
drat 0.11006076
wt   0.09265499
qsec 0.59351765

Upvotes: 2

Related Questions