I have a csv with the total agricultural area for each state and the area of different crops and I want to calculate the % area of each crop out of the total agricultural area and add it to table. Below is a simplified example of the csv.
| Ag_area | State | Pasture | Soy | Corn |
|---|---|---|---|---|
| 200 | WI | 50 | 50 | 25 |
| 100 | MN | 25 | 30 | 10 |
| 150 | IL | 100 | 0 | 50 |
I'm looking to produce a new table like this:
| Ag_area | State | Pasture | Soy | Corn | Pct_Pasture | Pct_Soy | Pct_Corn |
|---|---|---|---|---|---|---|---|
| 200 | WI | 50 | 50 | 25 | .25 | .25 | .125 |
| 100 | MN | 25 | 30 | 10 | .25 | .30 | .1 |
| 150 | IL | 100 | 0 | 50 | .33 | 0 | .166 |
I know how to code this for each column individually: df$Pct_Pasture <- df$Pasture/df$Ag_area
My dataset contains lots of crops so but I'm hoping there us a way to do it all in one shot. All of the crop columns are next to one another so I can use their index [3:89] and then ideally I'd be able to append 'Pct_' to each crop for the new calculated column. I'm thinking I need to use mutate but I can't figure it out? Thanks to anyone who takes a look at this