![](/img/trans.png)
[英]Calculate relative percentage (ratio) for groups in a dataframe using dplyr
[英]Using dplyr function to calculate percentage within groups
我有以下幾列:
人口 | 類型_n | 用戶 |
---|---|---|
2 | 小的 | 10 |
5 | 小的 | 11 |
7 | 中等的 | 12 |
7 | 中等的 | 13 |
9 | 大的 | 14 |
2 | 大的 | 15 |
4 | 大的 | 16 |
我想計算根據“type_n”定義的每個組內的百分比 - 即小組、中組和大組 - 作為“用戶”計數和“人口”總和之間比率的結果。 例如小組有 2 個用戶,人口總和為 7:(2/7)*100。
我想獲得這樣的輸出:
類型_n | 新列 |
---|---|
小的 | 28,5 |
中等的 | 14,2 |
大的 | 20 |
在此先感謝您的任何建議或幫助!
library(dplyr)
df %>%
# line below to freeze order of type_n if not ordered factor already
mutate(type_n = forcats::fct_inorder(type_n)) %>%
group_by(type_n) %>%
summarize(n = n(), total = sum(population)) %>%
mutate(new_col = (n / total) %>% scales::percent(decimal.mark = ",", suffix = ""))
# A tibble: 3 x 4
type_n n total new_col
<fct> <int> <int> <chr>
1 small 2 7 28,6
2 medium 2 14 14,3
3 large 3 15 20,0
使用base R
,將 ' rowsum
' 的table
與由 ' rowsum
' ed 的 'population' group
的rowsum
划分(這些組將按字母順序排列),並將命名向量輸出轉換為帶有stack
的兩列 data.frame
with(df1, stack(100 * table(type_n)/rowsum(population, type_n)[,1]))[2:1]
ind values
1 large 20.00000
2 medium 14.28571
3 small 28.57143
df1 <- structure(list(population = c(2L, 5L, 7L, 7L, 9L, 2L, 4L),
type_n = c("small",
"small", "medium", "medium", "large", "large", "large"), user = 10:16),
class = "data.frame", row.names = c(NA,
-7L))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.