Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply-with-na #8

Open
noamross opened this issue Mar 22, 2015 · 1 comment
Open

apply-with-na #8

noamross opened this issue Mar 22, 2015 · 1 comment

Comments

@noamross
Copy link
Owner

https://github.com/noamross/zero-dependency-problems/blob/master/R/apply-with-na.md

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!

@zmjones
Copy link

zmjones commented Oct 14, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants