My current project contains data that's got a structure like:
my_df <- tibble(
zn = c("hm","hm", "hm", "h60","h60","h60", "h85","h85","h85"),
nm = c("c", "cA", "cB", "c","cA", "cB", "c","cA", "cB"),
val = c(60, 40, 20, 250, 150, 100, 400, 250, 150),
znt = c(100, 100, 100, 300, 300, 300, 500, 500, 500),
)
my_df
# A tibble: 9 x 4
zn nm val znt
<chr> <chr> <dbl> <dbl>
1 hm c 60 100
2 hm cA 40 100
3 hm cB 20 100
4 h60 c 250 300
5 h60 cA 150 300
6 h60 cB 100 300
7 h85 c 400 500
8 h85 cA 250 500
9 h85 cB 150 500
And I would like to add a new variable, c0, whose value is defined c0 = znt - c for each zn. The final result would look like:
# A tibble: 12 x 4
zn nm val znt
<chr> <chr> <dbl> <dbl>
1 hm c0 40 100
2 hm c 60 100
3 hm cA 40 100
4 hm cB 20 100
5 h60 c0 50 300
6 h60 c 250 300
7 h60 cA 150 300
8 h60 cB 100 300
9 h85 c0 100 500
10 h85 c 400 500
11 h85 cA 250 500
12 h85 cB 150 500
I have an idea about how to do it but it seems very convoluted and I'm hoping there's a better way. If this could be accomplished using something from the tidyverse that'd be awesome, too.