0

I have this chunk of code:

trimmedMeans <- cleaned %>%
    filter(TrackName == t & ToBeTrimmed != 1) %>%
    group_by(TrackName, SpeakerName) %>%
    summarise(Expectation.Mean = mean(Expectation, na.rm = TRUE),
              Expectation.Sd = sd(Expectation, na.rm = TRUE),
              Interesting.Mean = mean(Interesting, na.rm = TRUE),
              Interesting.Sd = sd(Interesting, na.rm = TRUE),
              Useful.Mean = mean(Useful, na.rm = TRUE),
              Useful.Sd = sd(Useful, na.rm = TRUE),
              OralPresentation.Mean = mean(OralPresentation, na.rm = TRUE),
              OralPresentation.Sd = sd(OralPresentation, na.rm = TRUE),
              NumOfVoters = n()
    )

Now supposing to have this vector:

myvars <- c("Expectation", "Interesting", "Useful", "OralPresentation")

I'd like to generalize the previous chunk of code using the vector myvars dynamically.

lucazav
  • 858
  • 9
  • 24

1 Answers1

0

There is a summarise_at just for this:

iris
  %>% group_by(Species)
  %>% summarise_at(c("Sepal.Length", "Sepal.Width"), funs(mean, sd), na.rm=TRUE)

# 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
m0nhawk
  • 22,980
  • 9
  • 45
  • 73
  • 1
    Just to nit-pick;-) `summarise_at` is not that new (anymore), it replaced `summarise_each` in [May 2016](https://github.com/tidyverse/dplyr/issues/1845). – Maurits Evers Nov 29 '18 at 22:43
  • 1
    @MauritsEvers Ok, that's a nice notice. :D – m0nhawk Nov 29 '18 at 22:52
  • In my example there is also "NumOfVoters = n()". Is there a fast way to add this aggregate in the same statement? – lucazav Nov 30 '18 at 07:53
  • Solved in this way: `trimmedMeans <- cleaned %>% filter(TrackName == t & ToBeTrimmed != 1) %>% group_by(TrackName, SpeakerName) %>% add_tally() %>% group_by(TrackName, SpeakerName, n, add = FALSE) %>% summarise_at(.vars = speaker.characteristcs, funs(mean, sd), na.rm=TRUE)` – lucazav Nov 30 '18 at 08:06