How to output the difference of means in ttest

When I use 'test1[[3]]=t.test(data[[3]][,2],data[[3]][,3])[1:7]' to get the results of comparison of the mean difference between two samples, the difference in means should be -0.000750611667, but it just shows 0.
So how could I get the exact values of difference in means in ttest results? I think the process of calculating the difference in means separately and then combining it with the ttest results is too cumbersome. Thank you!

The "difference in means" you are pointing to is a sub-variable of "null.value". So I suspect what this means is

Under the null hypothesis, the expected difference in means in 0

You can actually find that in ?t.test, under the "Value" header:

null.value
the specified hypothesized value of the mean or mean difference depending on whether it was a one-sample test or a two-sample test.

So in your case, you'd want to look at the estimate entry:

diff(test1$estimate)

Thank you very much for your reply.
Yes,I actually want 'mean(x)-mean(y)', and diff(test1$estimate) works.

But it seems that this result is not directly displayed in the output of t.test, and it need 'diff' to calculate.
And what I care about is that this diff result cannot be output in the same data sheet as other results in t.test.

Indeed, this result doesn't seem to be directly displayed in the output of t.test, but I don't see why it's a problem, since it's easy enough to calculate.

How are you generating your output data sheet? Could you add an operation in the generating process? Would it solve all your problems to create a wrapper function around t.test like this:

augmented_t.test <- function(x,y, ...){
  res <- t.test(x,y, ...)
  
  list(mean_x = res$estimate[[1]],
       mean_y = res$estimate[[2]],
       diff = res$estimate[[2]] - res$estimate[[1]],
       statistic = res$statistic,
       p.value = res$p.value)
}

test2 <- augmented_t.test(data$x, data$y)

Or you could use broom::tidy() which does compute that estimate:

broom::tidy(test1)
# A tibble: 1 × 10
   estimate estimate1 estimate2 statistic p.value parameter  conf.low conf.high method                  alternative
      <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>     <dbl>     <dbl> <chr>                   <chr>      
1 -0.000764   0.00164   0.00240     -176.       0     1998. -0.000773 -0.000756 Welch Two Sample t-test two.sided  

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.