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)
This commit is contained in:
parent
c25ca46544
commit
b104592606
|
@ -528,7 +528,7 @@ As you start to write more functions, you'll eventually get to the point where y
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
wt_mean <- function(x, w) {
|
wt_mean <- function(x, w) {
|
||||||
sum(x * w) / sum(x)
|
sum(x * w) / sum(w)
|
||||||
}
|
}
|
||||||
wt_var <- function(x, w) {
|
wt_var <- function(x, w) {
|
||||||
mu <- wt_mean(x, w)
|
mu <- wt_mean(x, w)
|
||||||
|
@ -554,7 +554,7 @@ wt_mean <- function(x, w) {
|
||||||
if (length(x) != length(w)) {
|
if (length(x) != length(w)) {
|
||||||
stop("`x` and `w` must be the same length", call. = FALSE)
|
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]
|
x <- x[!miss]
|
||||||
w <- w[!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]
|
x <- x[!miss]
|
||||||
w <- w[!miss]
|
w <- w[!miss]
|
||||||
}
|
}
|
||||||
sum(w * x) / sum(x)
|
sum(w * x) / sum(w)
|
||||||
}
|
}
|
||||||
wt_mean(1:6, 6:1, na.rm = "foo")
|
wt_mean(1:6, 6:1, na.rm = "foo")
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue