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 have a data frame (tadlengths) with five columns of tadpole lengths for a bunch of different treatments. I want to average the five lengths. I did this by:
meanlength = apply(tadlengths, 1, mean)
However, sometimes I only had four lengths, so there are NAs in those rows. I want to average the four lengths on those rows, but the code above gives me "NA" for those rows. I feel like there is a very, very, easy solution for this, but I can't figure it out!
The text was updated successfully, but these errors were encountered:
I think the easiest way to teach this is to first show what the default behavior of mean is on a numeric vector with a NA. Then you could show how apply is a looping construct, so that the student learns to look at the function return object when they get output they didn't want. You could also use this to talk about anonymous functions.
https://github.com/noamross/zero-dependency-problems/blob/master/R/apply-with-na.md
The text was updated successfully, but these errors were encountered: