I just need a simple table of mean and sd by group


I have a data frame with 1 grouping column (group.factor) and 49 numeric variables. All I want to do is create a simple table with separate means and sds for each group. I’ve spent an inordinate amount of time trying to figure this out and could really use some help.

Also, I’ve tried this, and have no idea what the error even means.

 iris %>%
+     group_by(Species) %>%
+     summarise(across(starts_with("Sepal"), list(mean, sd), .names = "{col}.fn{fn}"))

Error: peek_mask() must only be used inside dplyr verbs
Run `rlang::last_error()` to see where the error occurred.

Any help or suggestions are greatly appreciated.

Jason the #rstatsnewbie

I dont think across is in the release version of dplyr yet...
so the current way might be like

iris %>%
       group_by(Species) %>%
       summarise_at(vars(starts_with("Sepal")), list(mean=mean, sd=sd))

result :

# A tibble: 3 x 5
  Species    Sepal.Length_mean Sepal.Width_mean Sepal.Length_sd Sepal.Width_sd
  <fct>                  <dbl>            <dbl>           <dbl>          <dbl>
1 setosa                  5.01             3.43           0.352          0.379
2 versicolor              5.94             2.77           0.516          0.314
3 virginica               6.59             2.97           0.636          0.322
1 Like

I think the error is more trivial - closing bracket should go in a different place, so the last line should read:

+     summarise(across(starts_with("Sepal")), list(mean, sd), .names = "{col}.fn{fn}")

Praise be to the flying spaghetti monster!!

Thank you so much!
You have no idea how much time and energy I’ve wasted trying to do this simple thing!
Thanks again!!!

Jason the #rstatsnewbie

1 Like

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