I would like to calculate the sum of each flower in each year in R. Below is an example of how the table looks (Table 1) and what I want the outcome to be (Table 2). I know how to do the code calculation in a long table format but I am not sure how to do it in a wide table format. Note: I am using package: dplyr
(Table 1)
| flower | 1902 | 1950 | 2010 | 2012 | 2021 |
|---|---|---|---|---|---|
| lily | 23 | 0 | 0 | 8 | 5 |
| rose | 50 | 60 | 5 | 16 | 0 |
| daisy | 30 | 7 | 10 | 2 | 0 |
I need to calculate the sum for each flower in each year. The end result should give me:
(Table 2)
| flower | 1902 | 1950 | 2010 | 2012 | 2021 |
|---|---|---|---|---|---|
| lily | 23 | 23 | 23 | 31 | 36 |
| rose | 50 | 110 | 115 | 131 | 131 |
| daisy | 30 | 37 | 47 | 49 | 49 |