I have a data frame that looks like below:
ST xa_2009 xa_2010 xa_2011 xp_2009 xp_2010 xp_2011 ya_2009 ya_2010 ya_2011 yp_2009 yp_2010 za_2009 MI 12 13 19 30 19 30 11 14 11 14 23 25 AZ 19 30 11 14 23 25 12 13 19 30 19 30 NY 11 14 19 30 19 30 11 14 23 25 12 13
The actual data has 700 rows and 250 columns in this same pattern.
I want to sum columns based on year (2009, 2010, 2011) and type (here the types are "a" and "p").
For example: xa_2009 + ya_2009 + za_2009, xa_2011+ ya_2011, xp_2009 + yp_2009 and so on..
so for this example, the final data frame should look like:
ST a2009 a2010 a2011 p2009 p2010 p2011
MI 48 27 30 44 42 30
AZ 61 43 30 44 42 25
NY 35 28 42 55 31 30
I am using Rstudio. So, I prefer the code to be written in R. Thank you in advance!
apply? you can create a vector containing all the names you need with a simple for loop. if you need more specific answers please usedput()to upload a reproducible example