okeoke
okeoke

Reputation: 93

How to reorder an output in t.test in R?

This is my first time using R, and I have a question regarding the output of t.test in R.

I am running the t.test function and getting an output like this:

data:  Age by Sex
t = -2.652, df = 710, p-value = 0.008181
alternative hypothesis: true difference in means between group female and group male is not equal to 0
95 percent confidence interval:
 -5.1887213 -0.7742204
sample estimates:
mean in group female   mean in group male 
            27.74517             30.72664 

Is there a way to individually call each of the various elements in the data? Something like test$data or test$mean so I can individually get the results of the output, and not have to output everything at once?

Thank you in advance.

Upvotes: 1

Views: 60

Answers (1)

GuedesBF
GuedesBF

Reputation: 9878

The t.test() function creates a list, which can be accessed like other lists:

test_result<-with(iris, t.test(Sepal.Length~Species=='setosa'))

> str(test_result)
List of 10
 $ statistic  : Named num 15.1
  ..- attr(*, "names")= chr "t"
 $ parameter  : Named num 147
  ..- attr(*, "names")= chr "df"
 $ p.value    : num 0.0000000000000000000000000000000771
 $ conf.int   : num [1:2] 1.09 1.42
  ..- attr(*, "conf.level")= num 0.95
 $ estimate   : Named num [1:2] 6.26 5.01
  ..- attr(*, "names")= chr [1:2] "mean in group FALSE" "mean in group TRUE"
 $ null.value : Named num 0
  ..- attr(*, "names")= chr "difference in means"
 $ stderr     : num 0.0829
 $ alternative: chr "two.sided"
 $ method     : chr "Welch Two Sample t-test"
 $ data.name  : chr "Sepal.Length by Species == \"setosa\""
 - attr(*, "class")= chr "test"

As you can see, there is an item called 'p.value', and another called "statistic". You can access it like this:

test_result$p.value

[1] 7.709331e-32
test_result$statistic
      t 
15.1441 

Upvotes: 1

Related Questions