I have a with users, the groups they belong to, and two columns I want to expand (below). I want to expand it so that each user keeps the groups that they're in but for each group they're in gets all possible permutations of country and season.
User_ID | Group | Country | Season |
---|---|---|---|
1 | 1 | France | Autumn |
2 | 2 | Spain | Summer |
1 | 2 | Italy | Winter |
3 | 2 | Italy | Spring |
Currently I'm expanding all of the columns then doing a semi-join to the table to drop all of the user and group combinations which don't exist.
df <- data.frame(User_ID = c(1, 2, 1, 3),
Group = c(1,2,2,1),
Country = c("France", "Spain", "Italy","Italy"),
Season = c("Summer", "Autumn", "Winter", "Spring"))
df %>%
expand(User_ID, Group, Country, Season) %>%
semi_join(df, by = c("User_ID", "Group")
Is this the most efficient way to do this, or am I missing something with the expand function?
CodePudding user response:
Is this what you want (4 user/groups x 3 countries x 4 seasons, i.e. 48 rows)?
library(tidyverse)
tribble(
~User_ID, ~Group, ~Country, ~Season,
1, 1, "France", "Autumn",
2, 2, "Spain", "Summer",
1, 2, "Italy", "Winter",
3, 2, "Italy", "Spring"
) |>
complete(nesting(User_ID, Group), Country, Season)
#> # A tibble: 48 × 4
#> User_ID Group Country Season
#> <dbl> <dbl> <chr> <chr>
#> 1 1 1 France Autumn
#> 2 1 1 France Spring
#> 3 1 1 France Summer
#> 4 1 1 France Winter
#> 5 1 1 Italy Autumn
#> 6 1 1 Italy Spring
#> 7 1 1 Italy Summer
#> 8 1 1 Italy Winter
#> 9 1 1 Spain Autumn
#> 10 1 1 Spain Spring
#> # … with 38 more rows
Created on 2022-06-14 by the reprex package (v2.0.1)