I have a dataframe of crash statistics called crashes_TA. The datafame looks like the following but on a much larger scale with each row representing a crash.
The dataframe is called crashes_TA
TA_name | TA_code | fatal_count | serious_injury_count | minor_injury_count | ID |
---|---|---|---|---|---|
Grey | 061 | 2 | 0 | 1 | 1 |
Buller | 062 | 1 | 1 | 1 | 2 |
Grey | 061 | 1 | 1 | 1 | 3 |
Clutha | 063 | 0 | 1 | 1 | 4 |
Clutha | 063 | 1 | 1 | 2 | 5 |
Otago | 064 | 1 | 1 | 0 | 6 |
I would like to summarise fatal, serious, and minor by TA_name by creating a new column called casualties. I would also like to summarise ID which represents the number of crashes per region as this value would be different to casualties as not all crashes have casualties. This new column would be called crashes
My new dataframe would then look like this:
TA_name | TA_code | fatal_count | serious_injury_count | minor_injury_count | casualties | crashes |
---|---|---|---|---|---|---|
Grey | 061 | 3 | 1 | 2 | 6 | 2 |
Buller | 062 | 1 | 1 | 1 | 3 | 1 |
Clutha | 063 | 1 | 2 | 3 | 6 | 2 |
Otago | 064 | 1 | 1 | 0 | 2 | 1 |
This is my code I have tried so far
crashes_stats_TA <- crashes_TA %>%
group_by(TA_code, TA_name) %>%
summarise(across(contains("count"), ~sum(., na.rm = T)),
across(Population, ~mean(., na.rm = T),
across(contains("perc"), ~mean(., na.rm = T), .names = "{.col}_mean"))) %>%
mutate(casualties = round(fatal_count + serious_injury_count + minor_injury_count),
crashes = round(ID = sum(ID, na.rm = T)))
However, when I do this I get this error:
Error: Problem with `mutate()` column `Crashes`.
i `Crashes = round(ID = sum(ID, na.rm = T))`.
x object 'ID' not found