From the course: Learning the R Tidyverse

Unlock the full course today

Join today to access over 24,100 courses taught by industry experts.

Column-wise groups: group_by() and summarize()

Column-wise groups: group_by() and summarize() - R Tutorial

From the course: Learning the R Tidyverse

Column-wise groups: group_by() and summarize()

- [Instructor] In the previous video, we saw how group_by() and mutate() work together. This video is going to look at mutate()'s sibling, summarise(), and how it works with group_by(). Let's do that in the 04_04b project. So in summarise.R script, we're loading the tidyverse on line one, and then we're selecting out three columns in the starwars dataset, species, homeworld, and height. And let's say that we're interested in the tallest individual from each species. We need to start answering that question with group_by() because we want to calculate statistics within the grouped species. So we'll group_by(species), and then we'll pipe this into summarise(), and we're going to create a new column, max_height. We'll use the function max() and the height column, and we're going to add na.rm is equal to TRUE to remove those NA values, and I'm going to run that code. Now, did you notice a difference between how mutate() worked in a previous video and summarise()? Let's copy and paste that…

Contents