R weighted arithmetic mean

Rodrigo picture Rodrigo · May 6, 2014 · Viewed 7.7k times · Source

Suppose I have this data.frame in R:

ages <- data.frame(Indiv = numeric(),
    Age = numeric(),
    W = numeric())
ages[1,] <- c(1,10,2)
ages[2,] <- c(1,15,5)
ages[3,] <- c(2,5,1)
ages[4,] <- c(2,100,2)

ages

  Indiv Age W
1     1  10 2
2     1  15 5
3     2   5 1
4     2 100 2

If I do:

meanAge <- aggregate(ages$Age,list(ages$Indiv),mean)

I get the mean Age (x) for each Indiv (Group.1):

  Group.1    x
1       1 12.5
2       2 52.5

But I want to calculate the weighted arithmetic mean of Age (weight being W). If I do:

WmeanAge <- aggregate(ages$Age,list(ages$Indiv),weighted.mean,ages$W)

I get:

Error in weighted.mean.default(X[[1L]], ...) : 
  'x' and 'w' must have the same length

I think I should have:

  Group.1           x
1       1 13.57142857
2       2 68.33333333

What am I doing wrong? Thanks in advance!

Answer

jaradniemi picture jaradniemi · May 6, 2014

Doh, you beat me to it. But anyway, here is my answer using both plyr and dplyr:

ages = data.frame(Indiv = c(1,1,2,2),
              Age = c(10,15,5,100),
              W = c(2,5,1,2))

library(plyr)
ddply(ages, .(Indiv), summarize, 
      mean = mean(Age),
      wmean = weighted.mean(Age, w=W))


library(dplyr)
ages %.% 
  group_by(Indiv) %.% 
  summarise(mean = mean(Age), wmean = weighted.mean(Age, W))