How to collapse multiple rows based on a column
dplyr group_by()
paste0 in R
In this tutorial, we will learn how to collapse multiple rows from a column to a single row based on another column/group.
Let us get started by loading tidyverse and checking the tidyr package version.
library(tidyvrerse)
packageVersion("tidyr")
## [1] '1.2.0'To illustrate collapsing multiple rows into single one, let us create a toy dataframe with continents and countries.
df
## 1 America USA
## 2 Europe England
## 3 America Canada
## 4 Asia Singapore
## 5 Europe France
## 6 Europe GermanyUsing group_by(), summarize(), and pasete0() we can collapse multiple rows into single row. Here we group by continent and summarize country by pasting with comma delimitter.
df %>%
group_by(continent) %>%
summarize(country=paste0(country, collapse = ", "))And this is how it looks after collapsing multiple rows belonging to different country from a continent.
## # A tibble: 3 × 2
## continent country
##
## 1 America USA, Canada
## 2 Asia Singapore
## 3 Europe England, France, Germany