Search code examples
rdplyrsummarize

Summarizing columns using a vector with dplyr


I want to calculate the mean of certain columns (names stored in a vector), while grouping against a column. Here is a reproducible example:

Cities <- c("London","New_York")
df <- data.frame(Grade = c(rep("Bad",2),rep("Average",4),rep("Good",4)),
                 London = seq(1,10,1), 
                 New_York = seq(11,20,1), 
                 Shanghai = seq(21,30,1))

> df
     Grade London New_York Shanghai
1      Bad      1       11       21
2      Bad      2       12       22
3  Average      3       13       23
4  Average      4       14       24
5  Average      5       15       25
6  Average      6       16       26
7     Good      7       17       27
8     Good      8       18       28
9     Good      9       19       29
10    Good     10       20       30

The output I want:

> df %>% group_by(Grade) %>% summarise(London = mean(London), New_York = mean(New_York))

# A tibble: 3 x 3
  Grade   London New_York
  <fct>    <dbl>    <dbl>
1 Average    4.5     14.5
2 Bad        1.5     11.5
3 Good       8.5     18.5

I would like to select the elements within vector cities (without calling their names) inside summarise, all while retaining their original name within the vector


Solution

  • You can do:

    df %>%
     group_by(Grade) %>%
     summarise_at(vars(one_of(Cities)), mean)
    
      Grade   London New_York
      <fct>    <dbl>    <dbl>
    1 Average    4.5     14.5
    2 Bad        1.5     11.5
    3 Good       8.5     18.5
    

    From documentation:

    one_of(): Matches variable names in a character vector.