Quick question, how to calculate group weights using dplyr
?
For example, given 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 result 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))
I'd 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 using count()
(only in dplyr >= 0.3) and prop.table()
:
D %>%
count(cat, wt = val) %>%
mutate(weight = prop.table(n))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With