I am trying to create a column called share which is calculated by dividing each value where noc_code is xxxx against noc_code=9999 WITHIN each industry group.
Here is a sample of my table
| Industry | noc_doe | VALUE |
|---|---|---|
| mining | 9999 | 100 |
| mining | 0001 | 15 |
| mining | 0005 | 33 |
| mining | 0941 | 50 |
| agriculture | 9999 | 100 |
| agriculture | 0701 | 25 |
| agriculture | 6785 | 4 |
| agriculture | 8456 | 90 |
my code is this, but it is not working
df %>%
group_by(Industry)%>%
mutate(Share = VALUE / VALUE[match("9999", noc_code)])-> df
I am hoping to get this result
| Industry | noc_doe | VALUE | Share |
|---|---|---|---|
| mining | 9999 | 100 | 1 |
| mining | 0001 | 15 | 0.15 |
| mining | 0005 | 33 | 0.33 |
| mining | 0941 | 50 | 0.5 |
| agriculture | 9999 | 100 | 1 |
| agriculture | 0701 | 25 | 0.25 |
| agriculture | 6785 | 4 | 0.04 |
| agriculture | 8456 | 90 | 0.9 |