From b104592606cb4cafb096564aa675b77918ee7859 Mon Sep 17 00:00:00 2001 From: Andrea Gilardi Date: Mon, 23 Jan 2017 16:05:54 +0100 Subject: [PATCH] Update Functions.Rmd (#526) I think there is a typo in wt_mean because if it's a weighted mean of x with weights w it should be sum(x * w) / sum(w) --- functions.Rmd | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/functions.Rmd b/functions.Rmd index 65bf7ed..f65be0e 100644 --- a/functions.Rmd +++ b/functions.Rmd @@ -528,7 +528,7 @@ As you start to write more functions, you'll eventually get to the point where y ```{r} wt_mean <- function(x, w) { - sum(x * w) / sum(x) + sum(x * w) / sum(w) } wt_var <- function(x, w) { mu <- wt_mean(x, w) @@ -554,7 +554,7 @@ wt_mean <- function(x, w) { if (length(x) != length(w)) { stop("`x` and `w` must be the same length", call. = FALSE) } - sum(w * x) / sum(x) + sum(w * x) / sum(w) } ``` @@ -577,7 +577,7 @@ wt_mean <- function(x, w, na.rm = FALSE) { x <- x[!miss] w <- w[!miss] } - sum(w * x) / sum(x) + sum(w * x) / sum(w) } ``` @@ -593,7 +593,7 @@ wt_mean <- function(x, w, na.rm = FALSE) { x <- x[!miss] w <- w[!miss] } - sum(w * x) / sum(x) + sum(w * x) / sum(w) } wt_mean(1:6, 6:1, na.rm = "foo") ```