group_by()

…and rename()

Ben Baumer

Still in dpylr

The Five Verbs

The Five Verbs

  • select()

  • filter()

  • mutate()

  • arrange()

  • summarize()

Plus:

  • group_by()

  • rename()

  • inner_join()

  • left_join()

summarize(): collapse into a single row

group_by(): group by a variable

Example: average fuel economy

mtcars %>%
  group_by(cyl) %>%
  summarize(
    num_cars = n(),
    avg_mpg = mean(mpg)
  )
# A tibble: 3 × 3
    cyl num_cars avg_mpg
  <dbl>    <int>   <dbl>
1     4       11    26.7
2     6        7    19.7
3     8       14    15.1

Summary funs

  • take a vector

  • output a single value

rename()

You can rename columns with rename. Here’s The first three rows of mtcars.

mtcars %>%
  head(3)
               mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710    22.8   4  108  93 3.85 2.320 18.61  1  1    4    1

Let’s rename cyl to cylinders

mtcars %>%
  rename(cylinders = cyl) %>%
  head(3)
               mpg cylinders disp  hp drat    wt  qsec vs am gear carb
Mazda RX4     21.0         6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag 21.0         6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710    22.8         4  108  93 3.85 2.320 18.61  1  1    4    1