You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to return a vector of same length that is the mean of each respective vector position, and also ignore the NA's (so take the average of one number, not two). I don't want NA's in my answer unless the same position in each vector has an NA.
So, the answer I want is:
c(2,3,7,8,10)
Anyone have an elegant solution to this problem?
The text was updated successfully, but these errors were encountered:
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.
https://github.com/noamross/zero-dependency-problems/blob/master/R/means-with-na.md
The text was updated successfully, but these errors were encountered: