user3022875
user3022875

Reputation: 9018

adding weighted average with aggregate

I have the data frame below

library(ggplot2)
v = c(100,50,50,20,30,100,100,100,40,5)
s = c(1,10, 5, 90,40, 100, 1,1,30,5)
g = c(rep("A",5),rep("B",5))
m = c (1,1,5,5,10, 10,10,8,5,2)
d = data.frame(value = v, size= s, group = g, m = m)
d
a  = aggregate(d$v, by = list(d$m, d$g), FUN= mean)
a
ggplot(data=a, aes(x=Group.1, y=x, group=Group.2)) +
  geom_line( )+geom_point()

Currently the aggregate() take the average "v" within each "g" and "m" group.

Instead of the simple average I'd like to plot the weighted average using "s" withing each "g" and "m" as the weight. For example in when g = A and m = 1 then the aggregate returns 75 which is average of 100 & 50 but I'd like to return the weighted average (100*1+50*10)/11 = 54.

What's the best way?

Thank you

Upvotes: 1

Views: 1123

Answers (1)

akrun
akrun

Reputation: 886938

We may use dplyr

library(dplyr)
d %>%
  group_by(m, group)  %>%
  summarise(vmean =  floor(weighted.mean(value, size)))
#     m  group vmean
#  <dbl> <fctr> <dbl>
#1     1      A    54
#2     2      B     5
#3     5      A    21
#4     5      B    40
#5     8      B   100
#6    10      A    30
#7    10      B   100

Or using base R

by(d[c("value", "size")], list(d$group, d$m), 
             FUN = function(x) weighted.mean(x[,1], x[,2]))

Upvotes: 1

Related Questions