I have a database with data for about 200 variables for every country in the world, for every year from 1970 to 2019 as a simplified example, the data frame would look something like this:
Data <- data.frame(
Country = c(rep("Aruba", 5),rep("Afghanistan",5), rep("Angola",5)),
Year = c(rep(c(2006:2010),3)),
var1 = c(11.4, 11.1, 10.4, 10.5, 9.98, 10.2, 9.54, 10.6, 11.1, 11.4, 10.7, 9.93, 11.0, 8.98, 10.9),
var2 = c(64.6, 64.7, 64.8, 65.1, 65.5, 66.1, 66.5, 67.1, 67.6, 68.1, 68.5, 68.8, 69.1, 69.5, 69.8)
)
I need to do operations on this database, which often include things like finding the change or a variable between two given years for each country In the example above, I could for example need to find the difference between 2007 and 2010 for var1 and var2 for each country, to produce something like this:
Country diff_var1 diff_var2
1 Aruba -1.12 0.8
2 Afghanistan 0.8 1.6
3 Angola 0.97 1
Is there any straightforward way of sub-setting the data by country, and then doing operations with the data filtered by year, on R? I don't think dplyr's "group_by" could work?