Home » R: How to Collapse Text by Group in Data Frame

R: How to Collapse Text by Group in Data Frame

by Tutor Aspire

You can use the following methods to collapse text by group in a data frame in R:

Method 1: Collapse Text by Group Using Base R

aggregate(text_var ~ group_var, data=df, FUN=paste, collapse='')

Method 2: Collapse Text by Group Using dplyr

library(dplyr)

df %>%
  group_by(group_var) %>%
  summarise(text=paste(text_var, collapse=''))

Method 3: Collapse Text by Group Using data.table

library(data.table)

dt data.table(df)

dt[, list(text_var=paste(text_var, collapse='')), by=group_var]

This tutorial explains how to use each method in practice with the following data frame:

#create data frame
df frame(team=c('A', 'A', 'A', 'B', 'B', 'B'),
                 position=c('Guard', 'Guard', 'Forward',
                            'Guard', 'Forward', 'Center'))

#view data frame
df

  team position
1    A    Guard
2    A    Guard
3    A  Forward
4    B    Guard
5    B  Forward
6    B   Center

Example 1: Collapse Text by Group Using Base R

The following code shows how to collapse the text in the position column, grouped by the team column using the aggregate() function from base R:

#collapse position values by team 
aggregate(position ~ team, data=df, FUN=paste, collapse='')

  team           position
1    A  GuardGuardForward
2    B GuardForwardCenter

Notice that each of the text values in the position column has been collapsed into one value, grouped by the values in the team column.

Example 2: Collapse Text by Group Using dplyr

The following code shows how to collapse the text in the position column, grouped by the team column using the summarise() function from the dplyr package:

library(dplyr)

#collapse position values by team
df %>%
  group_by(group_var) %>%
  summarise(text=paste(text_var, collapse=''))

# A tibble: 2 x 2
  team  text              
                
1 A     GuardGuardForward 
2 B     GuardForwardCenter

Notice that each of the text values in the position column has been collapsed into one value, grouped by the values in the team column.

Example 3: Collapse Text by Group Using data.table

The following code shows how to collapse the text in the position column, grouped by the team column using functions from the data.table package:

library(data.table)

#convert data frame to data table
dt data.table(df)

#collapse position values by team 
dt[, list(text_var=paste(text_var, collapse='')), by=group_var]

   team           position
1:    A  GuardGuardForward
2:    B GuardForwardCenter

Each of the text values in the position column has been collapsed into one value, grouped by the values in the team column.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Select Columns Containing a Specific String in R
How to Remove Characters from String in R
How to Find Location of Character in a String in R

You may also like