[英]R how to summarize the % breakdown of a column by other columns
我有一個這樣的數據框:
VisitID | No_Of_Visits | Store A | Store B | Store C | Store D|
A1 | 1 | 1 | 0 | 0 | 0 |
B1 | 2 | 1 | 0 | 0 | 1 |
C1 | 4 | 1 | 2 | 1 | 0 |
D1 | 3 | 2 | 0 | 1 | 0 |
E1 | 4 | 1 | 1 | 1 | 1 |
在 R 中,我如何總結數據框以按訪問計數 lvl 顯示每個商店類別的訪問百分比? 預期結果:
| No_Of_Visits | Store A | Store B | Store C | Store D|
| 1 | 100% | 0 | 0 | 0 |
| 2 | 50% | 0 | 0 | 50% |
| 3 | 67% | 0% | 33% | 0 |
| 4 | 25% | 38% | 25% | 13% |
我在考慮 group_by(No_Of_Visits) 和 mutate_all?
我們可以獲取長格式的數據並計算每個No_Of_Visits
和 Store 的總和,然后在將數據轉換為寬格式之前計算它們的比率。
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = starts_with('Store')) %>%
group_by(No_Of_Visits, name) %>%
summarise(value = sum(value)) %>%
mutate(value = round(value/sum(value) * 100, 2)) %>%
pivot_wider()
# No_Of_Visits Store.A Store.B Store.C Store.D
# <int> <dbl> <dbl> <dbl> <dbl>
#1 1 100 0 0 0
#2 2 50 0 0 50
#3 3 66.7 0 33.3 0
#4 4 25 37.5 25 12.5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.