1

I would like to generate overview tables for the same statistics (e.g., n, mean, sd) across multiple variables.

I started with combining the dyplr summarise and across function. See follwing example:

df <- data.frame(
  var1 = 1:10,
  var2 = 11:20
)
VarSum <- df %>% summarise(across(c(var1, var2), list(n = length, mean = mean, sd = sd)))

The output is of course given as one row (1x6) with three colums for each variable in this example. What I would like to achieve is to get the output rowise for each variable (2x3). Is that even possible with my approach? Would appriciate any suggestions.

Maël
  • 45,206
  • 3
  • 29
  • 67
Kilsen
  • 45
  • 5
  • Does this answer your question? [Reshaping data.frame from wide to long format](https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format) – Maël Feb 10 '23 at 09:07

1 Answers1

1

You can pivot first:

library(dplyr)
library(tidyr)
df %>% 
  pivot_longer(everything()) %>% 
  summarise(across(value, list(n = length, mean = mean, sd = sd)), .by = name)

  name  value_n value_mean value_sd
  <chr>   <int>      <dbl>    <dbl>
1 var1       10        5.5     3.03
2 var2       10       15.5     3.03
Maël
  • 45,206
  • 3
  • 29
  • 67
  • Thanks so much for your answer! When I apply your solution one to one, I get a 20x4 with `n`, `mean`, `sd` exactly the same and `var1` and `var2` alternating ten times. Did I miss something? – Kilsen Feb 09 '23 at 11:14
  • Do you have dplyr 1.1.0? otherwise, you should use `group_by` – Maël Feb 09 '23 at 11:15
  • 1
    That was it! After updating dplyr it worked! Thanks so much. :) – Kilsen Feb 09 '23 at 12:00