Open noamross opened 9 years ago
very related to #8.
you could start with the most conceptually simple solution, which is to loop over an index and compute the mean with na.rm = TRUE
, or just have them write their own mean function which has a condition which excludes missing data.
then you can have them bind the vectors and use apply, which also would allow them to learn about the margin argument.
again you could have them learn about anonymous functions here, e.g. function(x) ifelse(!all(is.na(x)), mean(x, na.rm = TRUE), NA).
also since mean(NA, na.rm = TRUE) == NaN
you could discuss the difference between the two.
Something like this?
colmean <- function(...) {
vecs <- list(...)
mat <- Reduce(rbind, vecs)
apply(mat, 2, function(col) {
na_col <- is.na(col)
if (all(na_col)) {
NA_real_
} else {
sum(col, na.rm = TRUE) / sum(!na_col)
}
})
}
colmean(
c(1,3,NA,7,9, NA),
c(3,NA,7,9,11, NA)
)
#> [1] 2 3 7 8 10 NA
Created on 2018-12-02 by the reprex package (v0.2.1)
https://github.com/noamross/zero-dependency-problems/blob/master/R/means-with-na.md