How to Simplify Results (or Not) with the sapply Function in R
How to Use Internal Functions in R
How to Use If…Else Statements in R

How to Extract Data Test Results with R

Many tests in R return a htest object. That type of object is basically a list with all the information about the test that has been carried out. All these htest objects contain at least an element statistic with the value of the statistic and an element p.value with the value of the p-value. You can see this easily if you look at the structure of the returned object.

The object returned by shapiro.test()looks like this:

> str(result)
List of 4
 $ statistic: Named num 0.933
 ..- attr(*, "names")= chr "W"
 $ p.value : num 7.76e-05
 $ method  : chr "Shapiro-Wilk normality test"
 $ data.name: chr "beaver2$temp"
 - attr(*, "class")= chr "htest"

Because this htest objects are lists, you can use any of the list subsetting methods to extract the information. The following code, for example, extracts the p-value from the t-test on the beaver data:

> t.test(temp ~ activ, data=beaver2)$p.value
[1] 7.269112e-31

The extraction of information from the htest object also works with the results of the other .test functions. You can check what kind of object a test returns by looking at the Help page for the test you want to use.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Count Unique Data Values in R
How to Use Factors or Numeric Data in R
How to Test Data Normality Graphically in R
How to Combine Logical Statements in R
How to Create a Lattice Plot in R
Advertisement

Inside Dummies.com