# Tidyr Package Cheatsheet
## Installing the package
```R
install.packages("tidyr")
library(tidyr)
billboard
- Song rankings for Billboard top 100 in the year 2000
library(tidyverse)
billboard_data <- tibble(
song = c("Song1", "Song2", "Song3"),
rank = c(1, 2, 3)
)
chop
- hopping and unchopping preserve the width of a data frame, changing its length. chop()
makes df
shorter by converting rows within each group into list-columns.
chopped_data <- chop(billboard_data)
unchopped_data <- unchop(chopped_data)
cms_patient_care
- Data from the Centers for Medicare & Medicaid Services
cms_patient_care_data <- cms_patient_care
cms_patient_experience
- Data from the Centers for Medicare & Medicaid Services
cms_patient_experience_data <- cms_patient_experience
complete
- Complete a data frame with missing combinations of data
complete_data <- complete(billboard_data, song = c("Song1", "Song2", "Song3"))
construction
- Completed construction in the US in 2018
construction_data <- construction
crossing
- Expand data frame to include all possible combinations of values
crossing_data <- crossing(x = c("A", "B"), y = c(1, 2))
drop_na
- Drop rows containing missing values
data_without_na <- drop_na(billboard_data)
expand
- Expand data frame to include all possible combinations of values
expanded_data <- expand(billboard_data, song = c("Song1", "Song2", "Song3"), rank = 1:3)
expand_grid
-Create a tibble from all combinations of inputs
expanded_grid_data <- expand_grid(song = c("Song1", "Song2", "Song3"), rank = 1:3)
extract
extract
- Extract a character column into multiple columns using regular expression groups
extracted_data <- extract(billboard_data, song, into = c("artist", "title"), regex = "(.*) - (.*)")
fill
- Fill in missing values with previous or next value
filled_data <- fill(billboard_data, rank)
fish_encounters
- Fish encounters
fish_encounters_data <- fish_encounters
full_seq
- Create the full sequence of values in a vector
full_sequence <- full_seq(1:5, period = 2)
gather
- Gather columns into key-value pairs
gathered_data <- gather(billboard_data, key = "attribute", value = "value", -song)
hoist
- Hoist values out of list-columns
hoisted_data <- hoist(tibble(x = list(1, 2, 3)))
household
- Household data
household_data <- household
nest
- Nest rows into a list-column of data frames
nested_data <- nest(billboard_data, data = -song)
nesting
- Expand data frame to include all possible combinations of values
nested_data_expanded <- nesting(billboard_data, song = c("Song1", "Song2", "Song3"), rank = 1:3)
nest_legacy
- Legacy versions of 'nest()' and 'unnest()'
nested_data_legacy <- nest_legacy(billboard_data, data = -song)
packed_data <- pack(billboard_data)
unpacked_data <- unpack(packed_data)
pivot_longer
- Pivot data from wide to long
longer_data <- pivot_longer(billboard_data, cols = rank, names_to = "position", values_to = "rank")
pivot_wider
- Pivot data from long to wide
wider_data <- pivot_wider(longer_data, names_from = "position", values_from = "rank")
population
- World Health Organization TB data
population_data <- population
relig_income
- Pew religion and income survey
relig_income_data <- relig_income
replace_na
- Replace NAs with specified values
data_with_replaced_na <- replace_na(billboard_data, list(rank = 0))
separate
- Separate a character column into multiple columns with a regular expression or numeric locations.
separated_data <- separate(billboard_data, col = song, into = c("artist", "title"), sep = " - ")
separate_longer_delim
- Split a string into rows
separated_longer_data <- separate_longer_delim(billboard_data, col = song, sep = ",")
separate_longer_position
- Split a string into rows
separated_longer_data <- separate_longer_position(billboard_data, col = song, sep = c(1, 3))
separate_rows
- Separate a collapsed column into multiple rows
separated_rows_data <- separate_rows(billboard_data, col = song, sep = ",")
separate_wider_delim
- Split a string into columns
separated_wider_data <- separate_wider_delim(billboard_data, col = song, sep = ",")
separate_wider_position
- Split a string into columns
separated_wider_data <- separate_wider_position(billboard_data, col = song, sep = c(1, 3))
separate_wider_regex
- Split a string into columns
separated_wider_data <- separate_wider_regex(billboard_data, col = song, regex = "(.*) - (.*)")
smiths
- Some data about the Smith family
spread
- Spread a key-value pair across multiple columns
spread_data <- spread(gathered_data, key = "attribute", value = "value")
table1
- Example tabular representations
table2
- Example tabular representations
table3
- Example tabular representations
table4a
- Example tabular representations
table4b
- Example tabular representations
table5
- Example tabular representations
uncount
- "Uncount" a data frame
uncounted_data <- uncount(billboard_data, wt = rank)
unite
- Unite multiple columns into one by pasting strings together
united_data <- unite(billboard_data, col = "combined", song, rank, sep = " - ")
unnest
- a list-column of data frames into rows and columns
unnested_data <- unnest(nested_data)
unnest_legacs
- Legacy versions of 'nest()' and 'unnest()'
unnested_data_legacy <- unnest_legacy(nested_data_legacy)
unnest_longer
- Unnest a list-column into rows
unnested_longer_data <- unnest_longer(nested_data)
unnest_wider
- Unnest a list-column into columns
unnested_wider_data <- unnest_wider(nested_data)
packed_data <- pack(billboard_data)
unpacked_data <- unpack(packed_data)
us_rent_income
- US rent and income data
us_rent_income_data <- us_rent_income
who
- World Health Organization TB data
who2
- World Health Organization TB data
world_bank_pop
- Population data from the World Bank
world_bank_pop_data <- world_bank_pop