3
$\begingroup$

I strictly need to use the summarise_at to compute a weighted mean, with weights based on the values of another column

    df %>% summarise_at(.vars = vars(FACTOR,tv:`smart tv/console`), 
                  .funs = weighted.mean, w=INVESTMENT, na.rm=TRUE)

It always shows the error: 'INVESTMENT' is not found.

I then tried with:

df %>%summarise_at(.vars = vars(FACTOR,tv:`smart tv/console`), 
               .funs = weighted.mean, w=vars(INVESTMENT), na.rm=TRUE)

But in this case : Evaluation error: 'x' and 'w' must have the same length.

Why is this? Am I doing anything wrong? Do you have hints to solve this issue? Thanks

$\endgroup$

2 Answers 2

2
$\begingroup$

You can specify the weights directly within the weighted.mean() function, within the call to funs() like so:

data.frame(x=rnorm(100), y=rnorm(100), weight=runif(100)) %>%
      summarise_at(vars(x,y), funs(weighted.mean(., w=weight)))
$\endgroup$
1
  • $\begingroup$ summarise_at has now been superseded by across. Your example code may now look something like this: data.frame(x=rnorm(100), y=rnorm(100), weight=runif(100)) %>% summarise(across(x:y, ~weighted.mean(., w = weight))) $\endgroup$
    – JPD
    Commented Sep 23, 2020 at 8:15
0
$\begingroup$

Reproducible Example of Weighted Mean:

df = data.frame(x=rnorm(6), y=rnorm(6), c=runif(6))
df
           x           y          c
1 -1.7474440 -0.31007446 0.05848928
2 -1.2224065  2.00200840 0.83287364
3 -0.2794127 -1.03566512 0.59140733
4 -0.3022496  0.05759183 0.05800232
5  0.4875809  0.88804664 0.36282380
6 -0.5468437  0.27501804 0.37218218

wt = c(5,5,4,1,3,5)/15 # This is the weight
wt
[1] 0.33333333 0.33333333 0.26666667 0.06666667 0.20000000
[6] 0.33333333

> weighted.mean(df$c,wt)
[1] 0.5992344

Weighted Mean R Documentation:

weighted.mean {stats}   R Documentation
Weighted Arithmetic Mean
Description
Compute a weighted mean.

Usage
weighted.mean(x, w, ...)

## Default S3 method:
weighted.mean(x, w, ..., na.rm = FALSE)
Arguments
x   
an object containing the values whose weighted mean is to be computed.

w   
a numerical vector of weights the same length as x giving the weights to use for elements of x.

... 
arguments to be passed to or from methods.

na.rm   
a logical value indicating whether NA values in x should be stripped before the computation proceeds.

Details
This is a generic function and methods can be defined for the first argument x: apart from the default methods there are methods for the date-time classes "POSIXct", "POSIXlt", "difftime" and "Date". The default method will work for any numeric-like object for which [, multiplication, division and sum have suitable methods, including complex vectors.

If w is missing then all elements of x are given the same weight, otherwise the weights coerced to numeric by as.numeric and normalized to sum to one (if possible: if their sum is zero or infinite the value is likely to be NaN).

Missing values in w are not handled specially and so give a missing value as the result. However, zero weights are handled specially and the corresponding x values are omitted from the sum.

Value
For the default method, a length-one numeric vector.

See Also
mean

Examples
## GPA from Siegel 1994
wt <- c(5,  5,  4,  1)/15
x <- c(3.7,3.3,3.5,2.8)
xm <- weighted.mean(x, wt)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.