output p value from a t-test in R

lukeg picture lukeg · Jul 3, 2015 · Viewed 16.6k times · Source

So lets work through the example from ?t.test()

We do a two-sample t-test on the data by:

t.test(1:10, y = c(7:20))

Now I am only interested in saving the p-value When I input the followng code, the $p.value is also saved.

t.test(1:10, y = c(7:20))[3]

I want only the p-value saved (with the $p.value) as an numeric/integer/double. Sorry for asking such a simple question

Answer

Stewart Macdonald picture Stewart Macdonald · Jul 3, 2015

You can save the p-value from the t-test to another variable with something like:

pVal <- t.test(1:10, y = c(7:20))$p.value

pVal will then be numeric:

> str(pVal)
num 1.86e-05