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!