Separate - Cghlewis/data-wrangling-functions GitHub Wiki
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 |
Separate into columns
Separate into rows
Separate into rows and columns
Main functions used in examples
| Package | Functions |
|---|---|
| tidyr | separate(); separate_rows(); separate_wider_delim(); separate_longer_delim() |
Other functions used in examples
| Package | Functions |
|---|---|
| dplyr | select() |
| stringr | str_split() |
| tidyr | unnest() |
Resources