Suppose I have the following R dataframe:
The Peril and Range columns are both factors. And I want to create a cumulative distribution column for Counts and Value like so:
How would I do this? I am using dplyr if that helps.
Assuming you have the data stored in df
this should work:
df %>%
group_by(Peril) %>%
mutate(
'Count CDF' = cumsum(Counts) / sum(Counts),
'Values CDF' = cumsum(Values) / sum(Values)
)
However your first and second table seem to have different counts and values for the 'Other' Peril.