I have a dataset like the following
| name | city | number |
|---|---|---|
| A | M | 2 |
| A | N | 3 |
| A | O | 5 |
| A | P | 7 |
| B | M | 7 |
| B | N | 8 |
| B | 0 | 9 |
| B | P | 2 |
For each of of the name category, I want to sum the number of M and N value and put it in a new variable. The same goes for O and P value. The dataset should look like the following:
| name | city | number |
|---|---|---|
| A | X | 5 |
| A | Y | 12 |
| B | X | 15 |
| B | Y | 11 |
I'm new in R programming. I have tried to use group by and mutate method but was not successful.
We could modify the values in the column 'city' to 'X', 'Y', and do a group by sum
-output
data