From 2e495e6dd37b19ae5a4ddf8ad4f62448d4dbde1f Mon Sep 17 00:00:00 2001 From: Nathan <139487362+nathanj3@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:28:16 -0500 Subject: [PATCH] Remove NA's from weighted mean Edit wtd_mean to add na.rm=TRUE by default, which will let summary_table return a non-NA value if there are NA's in the dataset --- R/SummaryStatistics.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/SummaryStatistics.R b/R/SummaryStatistics.R index 86a3593..ba84810 100644 --- a/R/SummaryStatistics.R +++ b/R/SummaryStatistics.R @@ -18,9 +18,9 @@ #' #' library(dplyr) #' illinois %>% wtd_mean(age, weight) -wtd_mean <- function(df, variable, weight){ +wtd_mean <- function(df, variable, weight, na.rm = TRUE){ df %>% - summarise(mean = weighted.mean(x = {{variable}}, w = {{weight}})) %>% + summarise(mean = weighted.mean(x = {{variable}}, w = {{weight}}, na.rm = na.rm)) %>% pull() }