Let's say I have a tibble like the following:
tib <- tribble(~id, ~var1_countrya, ~var2_countrya, ~var1_countryb, ~var2_countryb,
1, 1, 2, 1, 2,
2, 5, 3, 1, 3,
3, 6, 3, 1, 3
)
I'd like to create new columns delta_countrya = var2_countrya - var1_countrya
and delta_countryb = var2_countryb - var2_countryb
by selecting the pairs of variables using a regex and dplyr pipes. The final tibble should look like this
id var1_countrya var2_countrya delta_countrya var1_countryb var2_countryb delta_countryb
1 1 2 1 1 2 1
2 5 3 -2 1 3 2
3 6 3 -3 1 3 2
I've struggled to even set up the mutate clause in a way it creates multiple new variables, let alone finding the pairs of variables I'm looking for and taking the difference.