I have a dataframe like this:
data <- read.table(text="group; yr1; yr2; val
a; 1945; 1946; 20
a; 1945; 1946; 50
a; 1947; 1948; 40
b; 1926; 1927; 45
b; 1927; 1928; -10
b; 1927; 1928; -15 ", sep=";", header=T, stringsAsFactors = FALSE)
Whats the best way to sum up the column of val
for each pair of years by group so that the result would look like the following?
group yr1 yr2 val
a 1945 1946 70
a 1946 1947 40
b 1926 1927 45
b 1927 1928 -25