Suppose I have the following data:
data = tibble::tribble(
~id, ~year_1, ~year_2, ~cod_1, ~cod_2, ~cod_3, ~cod_4, ~var_x,
1, 0, 1, 5, 5, 3, 6, "x",
1, 0, 1, 3, 6, 14, 5, "x",
1, 0, 1, 2, 8, 5, 4, "x",
2, 1, 0, 10, 8, 2, 3, "x",
2, 1, 0, 3, 9, 1, 2, "x",
2, 1, 0, 1, 12, 0, 1, "x"
)
I'd like to create all posible products of the combination of all columns "year_" by all the columns "cod_". I mean something like this:
data.new = data %>%
mutate(year_1_cod_1 = year_1 * cod_1) %>%
mutate(year_1_cod_2 = year_1 * cod_2) %>%
mutate(year_1_cod_3 = year_1 * cod_3) %>%
mutate(year_1_cod_4 = year_1 * cod_4) %>%
mutate(year_2_cod_1 = year_2 * cod_1) %>%
mutate(year_2_cod_2 = year_2 * cod_2) %>%
mutate(year_2_cod_3 = year_2 * cod_3) %>%
mutate(year_2_cod_4 = year_2 * cod_4)
I can get all the possible combinations using:
year.var = colnames(data[, grepl("year", names(data))])
cod.var = colnames(data[, grepl("cod", names(data))])
com = crossing(year.var, cod.var)
> com
# A tibble: 8 x 2
year.var cod.var
<chr> <chr>
1 year_1 cod_1
2 year_1 cod_2
3 year_1 cod_3
4 year_1 cod_4
5 year_2 cod_1
6 year_2 cod_2
7 year_2 cod_3
8 year_2 cod_4
I could use a for loop to move over com data frame and create each new column. But a I'd like to do this inside dplyr:: environment. I think I can use purrr:: to mutate over all the combinations, but I am not sure how to.
In fact in my real data I have more than 1k possible combinations (i.e. more than 1k variables to mutate).