I have a dataset that looks like this:
a <- data.frame(cat = c("a","b","b","c","a","c"), num1 = c(-2,1,2,4,3,2), num2 = c(-2,3,1,5,3,2), weight = c(3.12,2,14,1.12,1,12))
I'm trying to get a weighted mean for each "num" column (on my real dataset I have many "num" columns, all named the same).
I started coding it like this:
a %>% summarise(num1 = weighted.mean(num1,weight,na.rm=T), num2 = weighted.mean(num2,weight,na.rm=T))
Which returns:
num1 | num2 |
---|---|
1.661853 | 1.394705 |
But since I have to process many columns I moved to this way:
a %>% summarise(across(contains("num"), ~weighted.mean(.x,na.rm=T)))
or:
a %>% summarise_at(.vars=vars(contains("num")), function(x,weight) weighted.mean(x,weight))
They both return:
num1 | num2 |
---|---|
1.666667 | 2 |
Why am I getting different results here? Which way should I trust?