Tidy Data with tidyr
Tidy data: each variable is a column, each observation a row, each value a cell.
Tidy data: each variable is a column, each observation a row, each value a cell.
19.1 Pivoting
library(tidyr)
wide <- tibble(country = c("A", "B"), `2023` = c(10, 20), `2024` = c(15, 25))
long <- wide |> pivot_longer(cols = `2023`:`2024`, names_to = "year", values_to = "value",
names_transform = list(year = as.integer))
long |> pivot_wider(names_from = year, values_from = value)
# Multiple variables encoded in column names
df |> pivot_longer(cols = -id, names_to = c("measure", "time"), names_sep = "_")
df |> pivot_longer(cols = -id, names_to = c(".value", "time"), names_sep = "_")19.2 Splitting & combining columns
df |> separate_wider_delim(full_name, delim = " ", names = c("first", "last"))
df |> separate_wider_regex(code, c(letters = "[A-Z]+", digits = "\\d+"))
df |> separate_longer_delim(tags, delim = ",")
df |> unite("date", year, month, day, sep = "-")19.3 Missing values
df |> drop_na(score)
df |> replace_na(list(score = 0, name = "unknown"))
df |> fill(group, .direction = "down") # carry last observation forward
df |> complete(year, country, fill = list(value = 0)) # make implicit missing explicit
expand_grid(x = 1:3, y = c("a", "b"))19.4 Nested data
nested <- penguins |> group_by(species) |> nest()
nested |> mutate(model = purrr::map(data, \(d) lm(body_mass_g ~ flipper_length_mm, data = d)),
r2 = purrr::map_dbl(model, \(m) summary(m)$r.squared))
nested |> unnest(data)
df |> unnest_wider(json_col); df |> unnest_longer(list_col); hoist(df, col, "field")