# t-statistic in descriptive summary

Hi everybody,
I would like to ask about command in R for the summary statistics. Beside mean, median, min, max., I would like to add t-statistic next to them. Which command should I use?

``````summary(mtcars\$mpg) -> a
t.test(mtcars\$mpg) -> b
a
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
#>   10.40   15.43   19.20   20.09   22.80   33.90
str(a)
#>  'summaryDefault' Named num [1:6] 10.4 15.4 19.2 20.1 22.8 ...
#>  - attr(*, "names")= chr [1:6] "Min." "1st Qu." "Median" "Mean" ...
b
#>
#>  One Sample t-test
#>
#> data:  mtcars\$mpg
#> t = 18.857, df = 31, p-value < 2.2e-16
#> alternative hypothesis: true mean is not equal to 0
#> 95 percent confidence interval:
#>  17.91768 22.26357
#> sample estimates:
#> mean of x
#>  20.09062
str(b)
#> List of 10
#>  \$ statistic  : Named num 18.9
#>   ..- attr(*, "names")= chr "t"
#>  \$ parameter  : Named num 31
#>   ..- attr(*, "names")= chr "df"
#>  \$ p.value    : num 1.53e-18
#>  \$ conf.int   : num [1:2] 17.9 22.3
#>   ..- attr(*, "conf.level")= num 0.95
#>  \$ estimate   : Named num 20.1
#>   ..- attr(*, "names")= chr "mean of x"
#>  \$ null.value : Named num 0
#>   ..- attr(*, "names")= chr "mean"
#>  \$ stderr     : num 1.07
#>  \$ alternative: chr "two.sided"
#>  \$ method     : chr "One Sample t-test"
#>  \$ data.name  : chr "mtcars\$mpg"
#>  - attr(*, "class")= chr "htest"
``````

Created on 2020-08-22 by the reprex package (v0.3.0)

Can we combine it into one output?

You can write a small function that does that.

``````STATS <- function(vec) {
a <- summary(vec)
b <- t.test(vec)\$statistic
c(a, b)
}
STATS(mtcars\$mpg)
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.        t
#> 10.40000 15.42500 19.20000 20.09062 22.80000 33.90000 18.85693
``````

Created on 2020-08-23 by the reprex package (v0.3.0)

3 Likes

Thank you so much. May I ask you one more question. Whether we can have such summary with all variables. I change the command to STATS(mtcars) but the output is not suitable.

The following code returns a matrix of values for the columns in mtcars. It throws an error if one of the columns is a factor. It would be possible to write code to prevent that but I doubt I could write a function that will gracefully handle all cases.

``````STATS_DF <- function(DF) {
STATS <- function(vec) {
a <- summary(vec)
b <- t.test(vec)\$statistic
c(a, b)
}
sapply(DF, STATS)
}

STATS_DF(mtcars)
``````
1 Like

Thank you so much for your help.
Best,

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