6

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

lukeg
  • 1,327
  • 3
  • 10
  • 27
  • 2
    Hi @lukeg. You might need to explain yourself more clearly. I don't understand what you're asking. `str(t.test(1:10, y = c(7:20))[3])` says that the value you're getting is already numeric. You can save that value to another variable with something like `pVal <- t.test(1:10, y = c(7:20))$p.value`. – Stewart Macdonald Jul 03 '15 at 11:25
  • I am running a loop and getting an error `Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' is a list, but does not have components 'x' and 'y'´ but by using the $p.value command, the error is elimainated. So thanks! – lukeg Jul 03 '15 at 11:29
  • 1
    Good to hear! I'll post my comment as a proper answer in keeping with the Way Things Are Done. – Stewart Macdonald Jul 03 '15 at 11:30
  • Don't apologize. R has truly terrible documentation of object attributes. – jessexknight Apr 05 '20 at 00:13

2 Answers2

7

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
Stewart Macdonald
  • 2,062
  • 24
  • 27
0
test <- t.test(df$column1, df$column2, paired = TRUE)

test$p.value
Noah Sheldon
  • 1,461
  • 10
  • 12