r - Calculate cumulative (100%) percentage from column cumulative range
问题描述
I have a unique question about calculating cumulative percentages in my dataframe below.
dtFMCount <- c(6, 21, 3, 11, 1, 3, 44, 2, 4, 1)
dtTotDur <- c(607142.353, 96240.799, 79624.690, 78672.113, 30265.219, 15037.119, 14870.920, 8470.878, 7305.159, 4126.279)
DF <- data.frame(dtFMCount, dtTotDur)
summ <- sum(DF$dtTotDur)
DF$Cumm <- round((100 * DF$dtTotDur/summ), digits = 2)
Using the above code I can get a DF like this with the DT$Cumm column as a cumalitve of the summ of DF$dtTotdur.
What I wanting to do is add another column DF$Cumm2 that would show a cumulative based of a 100% scale. I can do in excel see below.
So, I have tried a few different things, but it does not give me what I want. The below code and variations of it but not getting the result.
DF <- DF %>% group_by(dtFMCount, dtTotDur) %>% summarise(proportion = n()) %>%
mutate(Perc = cumsum(100*proportion/sum(proportion))) %>%
select(-proportion)
Any help with this in either a data.frame or data.table formats would help and Thanks for your help all.
解决方案
我们可以做的
library(dplyr)
df %>%
mutate(New = cumsum(Cumm))
推荐阅读
- python - 如何从多个 .txt 文件中创建唯一的数据框?
- python - 在 pysnopper 中调试 python 的最佳方法
- angular - 如何使用 Leaflet 在点击时设置新的纬度和经度?
- azure - Azure 虚拟机 SSL 证书
- r - eval 中的错误(predvars,data,env):找不到对象“客户计数”
- hibernate - 创建名为“entityManagerFactory”的bean时出错-Spring boot不接受我的entityManager
- ruby-on-rails - 预览视频附件 Rails Admin, Active Storage
- python - 如何从存储桶中下载 csv 文件然后在函数中使用?
- python - 根据列元素中的单词(部分匹配或完整)将多个标签匹配到列
- sass - Sass:如何获取当前 .sass 文件的文件系统的绝对路径?