Suppose I have the following dataset:
data.frame(country = c("A", "A", "B", "B", "C", "C"), type = c("x", "z", "x", "z", "x", "z"), value = c(4, 2, 6, 10, 54, 10))
country type value1 A x 42 A z 23 B x 64 B z 105 C x 546 C z 10
What I want to do is to create multiple new rows for each country (A, B, C) with a new type called "z-x" and a new value which is the difference between the values z and x. I'm looking for this output:
data.frame(country = c("A", "A", "A", "B", "B", "B", "C", "C", "C"), type = c("x", "z", "z-x", "x", "z", "z-x", "x", "z", "z-x"), value = c(4, 2, -2, 6, 10, 4, 54, 10, -44))
country type value1 A x 42 A z 23 A z-x -24 B x 65 B z 106 B z-x 47 C x 548 C z 109 C z-x -44