tidyverse / funs

Collection of low-level functions for working with vctrs
Other
34 stars 7 forks source link

need weighted_mean() to avoid common problem #27

Open behrman opened 5 years ago

behrman commented 5 years ago

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.