Dplyr: Calculate group weights
A quick question is how to calculate the weight of groups using dplyr
?
For example, data:
D = data.frame(cat=rep(LETTERS[1:2], each=2), val=1:4)
# cat val
# 1 A 1
# 2 A 2
# 3 B 3
# 4 B 4
The desired output should be:
# cat weight
# 1 A 0.3 # (1+2)/10
# 2 B 0.7 # (3+4)/10
Anything more succinct than the following?
D %>%
mutate(total=sum(val)) %>%
group_by(cat) %>%
summarise(weight=sum(val/total))
+3
source to share
1 answer
I would write it as
D <- data.frame(
cat = rep(LETTERS[1:2], each = 2),
val = 1:4
)
D %>%
group_by(cat) %>%
summarise(val = sum(val)) %>%
mutate(weight = val / sum(val))
Which you can simplify a little by using count()
(only in dplyr> = 0.3) and prop.table()
:
D %>%
count(cat, wt = val) %>%
mutate(weight = prop.table(n))
+8
source to share