Spread Columns into Wider Dataset (3/3): Generate new columns from multiple variables
In this section, we will discuss how to widen datasets by generating new columns from multiple variables in pivot_wider(). We’ll demonstrate the variable combinations in names_from and values_from respectively using the following two examples.
e.g.1.
library(tidyr)library(dplyr) production <-expand_grid(product =c("A", "B"), country =c("UK", "USA"), year =2023:2025) %>%filter((product =="A"& country =="UK") | product =="B") %>%mutate(production =rnorm(nrow(.))) production
Output:
# A tibble: 9 × 4 product country year production <chr> <chr> <int> <dbl> 1 A UK 2023 -0.237 2 A UK 2024 -0.971 3 A UK 2025 -1.53 4 B UK 2023 0.153 5 B UK 2024 0.599 6 B UK 2025 -2.08 7 B USA 2023 1.14 8 B USA 2024 -2.01 9 B USA 2025 -0.913
We want to widen the data so we have one column for each combination of product and country. The key is to specify multiple variables in names_from.
production %>%pivot_wider(# create new column names as combination of "product" and "country"names_from =c(product, country), values_from = production)
When either names_from or values_from select multiple variables, you can control how the column names in the output are constructed with arguments names_sep and names_prefix.
production %>%pivot_wider(names_from =c(product, country), values_from = production,names_sep =".", # separate parts of the column name with a dotnames_prefix ="prod."# add prefix "prod.")
Alternatively, you can construct new column names using the argument names_glue. Here prod_{product}_{country} combines prefix “prod” with unique levels of the product and country variables, connected with underscores.
production %>%pivot_wider(names_from =c(product, country), values_from = production,names_glue ="prod_{product}_{country}"# specify new column name pattern)