Skip to content

Separate

Cghlewis edited this page Dec 19, 2022 · 20 revisions

These functions cover two scenarios.

  1. You have a column that contains multiple delimited values and you want to split those into multiple columns.
  2. You have a column that contains multiple delimited values and you want to split those into multiple rows.

For example, if you collect data on pizza toppings and you receive data such as this.

Name Toppings
Dan anchovies, tomatoes, garlic
Crystal sausage, red peppers
Fox cheese

You can split Toppings into columns (wide data)

Name Topping1 Topping2 Topping3
Dan anchovies tomatoes garlic
Crystal sausage red peppers
Fox cheese

Or you can split Toppings into rows (long data)

Name Toppings
Dan anchovies
Dan tomatoes
Dan garlic
Crystal sausage
Crystal red peppers
Fox cheese

Separate into columns

Separate into rows


Main functions used in examples

Package Functions
tidyr separate(); separate_rows()

Other functions used in examples

Package Functions
dplyr select()
stringr str_split()
tidyr unnest()

Resources

Clone this wiki locally