I teach classes where students learn and use the tidyverse, and I've been noticing that a large proportion are getting wrong results from weighted.mean() without knowing it.
The students learn to use dplyr::count() with the wt argument. When they use weighted.mean(), they also use the wt argument. The weights argument for weighted.mean() is w not wt, and since is uses ..., those who use wt don't receive an error message. Instead, weighted.mean() returns the unweighted mean, which is almost certainly not what the user intended.
May I suggest a new tidyverse function weighted_mean() that uses wt for its weights argument to be consistent with count() and that gives an error message when used incorrectly.
I teach classes where students learn and use the tidyverse, and I've been noticing that a large proportion are getting wrong results from
weighted.mean()
without knowing it.The students learn to use
dplyr::count()
with thewt
argument. When they useweighted.mean()
, they also use thewt
argument. The weights argument forweighted.mean()
isw
notwt
, and since is uses...
, those who usewt
don't receive an error message. Instead,weighted.mean()
returns the unweighted mean, which is almost certainly not what the user intended.May I suggest a new tidyverse function
weighted_mean()
that useswt
for its weights argument to be consistent withcount()
and that gives an error message when used incorrectly.