-
Notifications
You must be signed in to change notification settings - Fork 16
Separate
Cghlewis edited this page Dec 19, 2022
·
20 revisions
These functions cover two scenarios.
- You have a column that contains multiple delimited values and you want to split those into multiple columns.
- 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 |
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