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?