[英]R Add CDF Columns to DataFrame
Suppose I have the following R dataframe:假设我有以下 R dataframe:
The Peril and Range columns are both factors. Peril 和 Range 列都是因素。 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.
如果有帮助,我正在使用 dplyr。
Assuming you have the data stored in df
this should work:假设您将数据存储在
df
中,这应该可以:
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.但是,您的第一个和第二个表似乎对“其他”危险具有不同的计数和值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.