Question Details

No question body available.

Tags

r dplyr aggregate summary t-test

Answers (2)

Accepted Answer Available
Accepted Answer
July 28, 2026 Score: 4 Rep: 26,843 Quality: Expert Completeness: 80%

I know of a few ways to calculate statistics for multiple variables in a table, but I'm not sure if I've understood your expected output for your example; does this approach using the rstatix package give you your desired outcome?

library(tidyverse)
library(rstatix)

stats % filter(cyl == 4 | cyl == 8) %>% groupby(cyl) %>% getsummarystats(type = "meansd") %>% pivotwider(namesfrom = cyl, valuesfrom = c(n, mean, sd), namesglue = "{.value} (cyl={cyl})") stats #> # A tibble: 10 × 7 #> variable n (cyl=4) n (cyl=8) mean (cyl=4) mean (cyl=8) sd (cyl=4) #> #> 1 mpg 11 14 26.7 15.1 4.51 #> 2 disp 11 14 105. 353. 26.9 #> 3 hp 11 14 82.6 209. 20.9 #> 4 drat 11 14 4.07 3.23 0.365 #> 5 wt 11 14 2.29 4.00 0.57 #> 6 qsec 11 14 19.1 16.8 1.68 #> 7 vs 11 14 0.909 0 0.302 #> 8 am 11 14 0.727 0.143 0.467 #> 9 gear 11 14 4.09 3.29 0.539 #> 10 carb 11 14 1.54 3.5 0.522 #> # ℹ 1 more variable: sd (cyl=8)

pvals % filter(cyl == 4 | cyl == 8) %>% pivotlonger(-cyl) %>% groupby(name) %>% ttest(data = ., formula = value ~ cyl) pvals #> # A tibble: 10 × 9 #> name .y. group1 group2 n1 n2 statistic df p #> * #> 1 am value 4 8 11 14 3.42 18.5 2.97e- 3 #> 2 carb value 4 8 11 14 -4.39 16.5 4.2 e- 4 #> 3 disp value 4 8 11 14 -12.5 17.8 3.03e-10 #> 4 drat value 4 8 11 14 5.67 21.8 1.10e- 5 #> 5 gear value 4 8 11 14 3.18 23.0 4.18e- 3 #> 6 hp value 4 8 11 14 -8.43 18.1 1.11e- 7 #> 7 mpg value 4 8 11 14 7.60 15.0 1.64e- 6 #> 8 qsec value 4 8 11 14 3.94 17.4 1 e- 3 #> 9 vs value 4 8 11 14 10 10 1.59e- 6 #> 10 wt value 4 8 11 14 -6.44 23.0 1.42e- 6

combined % leftjoin(pvals, by = c("variable" = "name")) %>% select(-c(.y., group1, group2, n1, n2)) kableExtra::kable(combined)

variable n (cyl=4) n (cyl=8) mean (cyl=4) mean (cyl=8) sd (cyl=4) sd (cyl=8) statistic df p
mpg 11 14 26.664 15.100 4.510 2.560 7.596664 14.96675 1.60e-06
disp 11 14 105.136 353.100 26.872 67.771 -12.496797 17.79660 0.00e+00
hp 11 14 82.636 209.214 20.935 50.977 -8.429940 18.09582 1.00e-07
drat 11 14 4.071 3.229 0.365 0.372 5.668243 21.80614 1.10e-05
wt 11 14 2.286 3.999 0.570 0.759 -6.444974 22.97119 1.40e-06
qsec 11 14 19.137 16.772 1.682 1.196 3.944600 17.40676 1.00e-03
vs 11 14 0.909 0.000 0.302 0.000 10.000000 10.00000 1.60e-06
am 11 14 0.727 0.143 0.467 0.363 3.416883 18.53711 2.97e-03
gear 11 14 4.091 3.286 0.539 0.726 3.179761 22.95334 4.18e-03
carb 11 14 1.545 3.500 0.522 1.557 -4.393954 16.54979 4.20e-04

Created on 2026-07-28 with reprex v2.1.1.9000

July 28, 2026 Score: 3 Rep: 7,628 Quality: Medium Completeness: 80%

another approach:

library(dplyr)
library(tidyr)

mtcars |> filter(cyl %in% c(4, 8)) |> pivotlonger(cols = -cyl, namesto = "variable" ) |> mutate(p = t.test(value ~ cyl)$p.value, .by = c(variable) ) |> summarise(p = first(p), mean = mean(value), sd = sd(value), .by = c(variable, cyl) ) |> pivotwider(idcols = c(variable, p), namesfrom = cyl, valuesfrom = c(mean, sd) )

output:

# A tibble: 10 × 6
   variable        p  mean4  mean8   sd4   sd8

1 mpg 1.64e- 6 26.7 15.1 4.51 2.56 2 disp 3.03e-10 105. 353. 26.9 67.8 3 hp 1.11e- 7 82.6 209. 20.9 51.0 4 drat 1.10e- 5 4.07 3.23 0.365 0.372 5 wt 1.42e- 6 2.29 4.00 0.570 0.759 6 qsec 1.00e- 3 19.1 16.8 1.68 1.20 7 vs 1.59e- 6 0.909 0 0.302 0 8 am 2.97e- 3 0.727 0.143 0.467 0.363 9 gear 4.18e- 3 4.09 3.29 0.539 0.726 10 carb 4.20e- 4 1.55 3.5 0.522 1.56

If you need to summarize (incl. tests) and format output on a regular base, package gtsummary might be helpful.