0

Example data:

library(data.table)
set.seed(1)
DT <- data.table(panelID = sample(50,50),                                                    # Creates a panel ID
                      Country = c(rep("Albania",30),rep("Belarus",50), rep("Chilipepper",20)),       
                      some_NA = sample(0:5, 6),                                             
                      some_NA_factor = sample(0:5, 6),         
                      Group = c(rep(1,20),rep(2,20),rep(3,20),rep(4,20),rep(5,20)),
                      Time = rep(seq(as.Date("2010-01-03"), length=20, by="1 month") - 1,5),
                      wt = 15*round(runif(100)/10,2),
                      Income = round(rnorm(10,-5,5),2),
                      Happiness = sample(10,10),
                      Sex = round(rnorm(10,0.75,0.3),2),
                      Age = sample(100,100),
                      Educ = round(rnorm(10,0.75,0.3),2))           
DT [, uniqueID := .I]                                                                        # Creates a unique ID                                                                                # https://stackoverflow.com/questions/11036989/replace-all-0-values-to-na
DT$some_NA_factor <- factor(DT$some_NA_factor)

I would like to calculate the weighted mean of all numerical columns, so I tried:

DT_w <- DT[,lapply(Filter(is.numeric,.SD), function(x) weighted.mean(DT$wt, x, na.rm=TRUE)), by=c("Country", "Time")]

But then it says:

Error in weighted.mean.default(DT$wt, x, na.rm = TRUE) : 
  'x' and 'w' must have the same length

I think I am perhaps misunderstanding the syntax. Am I doing this right?

Tom
  • 2,173
  • 1
  • 17
  • 44

1 Answers1

1

Two issues:

  • when you use DT$wt that is an explicit call to the full wt column from the DT table - the by arguments won't work on it. The by arguments will only work on columns without the DT$ prefix.

  • The order of arguments for weighted.mean() is x first and w (weights) second - you seem to have this backwards

Fixing those two issues:

DT_w <- DT[,lapply(Filter(is.numeric,.SD), function(x) weighted.mean(x, w = wt, na.rm=TRUE)), by=c("Country", "Time")]
# runs without errors
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294