[R] Weighted Average on More than One Variable in Data Frame
StellathePug
ritacarreira at hotmail.com
Thu Sep 22 00:19:53 CEST 2011
Thanks Jean, that worked perfectly!
Try this
sapply(split(df, df$g), function(x) apply(x[, 1:2], 2, weighted.mean,
x$w))
Jean
StellathePug wrote on 09/21/2011 01:15:33 PM:
>
> I have a data frame, say df, which has 4 variables, one of which I would
> like to use as a grouping variable (g), another one that I would like to
> use
> for my weights (w) The other two variables are variables (x1 and x2) for
> which I would like to compute the weighted average by group.
>
> df <- data.frame(x1 = c(15, 12, 3, 10, 10, 14, 12),
> x2 = c(10, 11, 16, 9, 7, 17, 18),
> g = c( 1, 1, 1, 2, 2, 3, 3),
> w = c( 2, 3, 1, 5, 5, 2, 5))
>
> wx1 <- sapply(split(df, df$g), function(x){weighted.mean(x$x1, x$w)})
> wx2 <- sapply(split(df, df$g), function(x){weighted.mean(x$x2, x$w)})
>
> The above code works, the result is:
> > wx1
> 1 2 3
> 11.50000 10.00000 12.57143
> > wx2
> 1 2 3
> 11.50000 8.00000 17.71429
>
> But is there not a more elegant way of acting on x1 and x2
simultaneously?
> Something along the lines of
>
> wdf <- sapply(split(df, df$g), function(x){weighted.mean(df, x$w)})
>
> which is wrong since df has two columns, while w only has one. I
suppose, one could write a loop but that strikes me as being highly
inefficient.
--
View this message in context: http://r.789695.n4.nabble.com/Weighted-Average-on-More-than-One-Variable-in-Data-Frame-tp3830922p3831611.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list