0

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?

S.R.
  • 25
  • 7
  • 1
    please make your code reproducible, what's in `data1` ? – mtoto Mar 19 '16 at 10:27
  • data1 is a table with 50 observations from soil managements in rows for several parameters (nitrogen, phosphate, carbon, which I called "A", "B", "C" and so on in the columns). I´d like to apply the test over all columns and geht only the p-values as results. So said, the p-values for every column in a vector or list. – S.R. Mar 19 '16 at 10:54
  • Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Heroka Mar 19 '16 at 11:15
  • `dput()` your data instead of describing it, along with your expected output. – mtoto Mar 19 '16 at 11:21

1 Answers1

2

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
spsaaibi
  • 452
  • 4
  • 13