I have a (large) dataframe of the form:
Variable Country 2007-Q1 2007-Q2 2007-Q3 2007-Q4 2008-Q1 2008-Q2 2008-Q3 2008-Q4
Var1 AR:Argentina 69.8 67.3 65 63.6 60.4 56.6 54.4 57.3
Var2 AR:Argentina 191.298 196.785 196.918 207.487 209.596 219.171 216.852 213.124
Var3 AR:Argentina 594.67 606.157 620.783 652.59 662.784 663.191 676.188 735.065
Var4 AR:Argentina 49.6 47.5 45.2 44.4 41.7 38.7 36.8 39.3
Var5 AR:Argentina 135.971 138.885 137.005 144.903 144.757 149.803 146.492 146.102
Var6 AR:Argentina 422.679 427.808 431.909 455.75 457.752 453.288 456.791 503.906
Var8 AR:Argentina 9.657 10.755 11.856 13.267 14.47 16.523 16.727 16.235
Essentially, every row has 4 columns of data for each year, sorted by quarter. I want to turn this into annual data.
One way to do this is to simply sum up every 4 columns (so 2008-Q1:2008-Q4 would be summed, for example). Another way, I suppose, would be to filter columns that share a common year in (2007-** or something) and then run RowSums()
on them but this sounds like it would be much more inefficient.
I'm hoping to get back a data frame that looks like:
Variable Country 2007 2008
Var1 AR:Argentina SUMXX SUMXX
Or even better:
Country Year Var1 Var2
AR:Argentina 2007 SUMXX SUMYY
AR:Argentina 2008 SUMXX SUMYY
.
The second format would be much preferred but the first format is fine as well. The main thing is that I need to be able to select the data for a variable, for a country, for all years -relatively easily. If I can select for all countries all years for any given variable - even better (second format).
Is there any easy way to accomplish this, aside from running a nested loop etc?