vendredi 11 juin 2021

Add values of columns based on condition of another variable in R

I want to create a variable that adds the values from other columns based on the condition of variable year. That is if variable YEAR = 2013 then add columns YR_2006, YR_2007, YR_2008, YR_2009, YR_2010, YR_2011. So for group A the sum would be 12,793

GROUP   YEAR    YR_2006 YR_2007 YR_2008 YR_2009 YR_2010 YR_2011
A       2013            636     3653    4759    3745
B       2019    1417    2176    3005    2045    2088    1849
C       2007    4218    3622    4651    4574    4122    4711
E       2017    5956    6031    6032    4885    5400    5828

Aucun commentaire:

Enregistrer un commentaire