I have this tibble:
# A tibble: 2 x 8
a.x b.x c.x d.x a.y b.y c.y d.y
<int> <int> <int> <int> <int> <int> <int> <int>
1 13 13 12 11 7 1 4 2
2 17 11 0 0 16 2 0 0
df <- structure(list(a.x = c(13L, 17L), b.x = c(13L, 11L), c.x = c(12L,
0L), d.x = c(11L, 0L), a.y = c(7L, 16L), b.y = 1:2, c.y = c(4L,
0L), d.y = c(2L, 0L)), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"))
I want to calculate: a.x - a.y, b.x - b.y, c.x - c.y, and so on ....
My desired output:
a.x b.x c.x d.x a.y b.y c.y d.y a b c d
<int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 13 13 12 11 7 1 4 2 6 12 8 9
2 17 11 0 0 16 2 0 0 1 9 0 0
I can achieve this by:
df %>%
mutate(a = a.x-a.y,
b = b.x-b.y,
c = c.x-c.y,
d = d.x-d.y)
I want to learn:
- How to extract the prefixes to new column names.
- Automate the calculation
.x - .y.