0

I have a dataframe that looks like this:

Year Type Budget
2010 x1 100
2010 x3 200
2010 x2 100
2010 y1 200
2010 y3 100
2010 z1 200
2011 z1 50
2011 x2 250
2012 x1 600
2012 x3 800
2013 y2 100
2013 y3 700
2013 x1 800

(The x's, y's and z's are not really there in the real data, it is just to give an example.)

I want to summarise the budget by grouping specific values for the type. We could imagine that I would want to summarise the budget for all types starting with x, y and z for each year, such that we get:

Year Type Budget
2010 x 400
2010 y 300
2010 z 200
2011 z 50
2011 x 250
2012 x 1400
2013 y 800
2013 x 800

Because the x's, y's and z's are fictive, I would need to specify what specific values of 'type' that would go each of the summarised 'types'. E.g.: Imagine that x1 is bread, x2 is butter and the summarising type is 'groceries', while z1 is vacuum cleaner, z2 earphones and the summarising type is 'electronics'.

How could this be solved in R?

  • 1
    Does this answer your question? [How to sum a variable by group](https://stackoverflow.com/questions/1660124/how-to-sum-a-variable-by-group) – user438383 Jul 01 '21 at 15:57

0 Answers0