Navigation Menu

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

create use_first_valid_of function #32

Closed
sfirke opened this issue Jul 5, 2016 · 3 comments
Closed

create use_first_valid_of function #32

sfirke opened this issue Jul 5, 2016 · 3 comments

Comments

@sfirke
Copy link
Owner

sfirke commented Jul 5, 2016

For saying, use A if not NA, otherwise use B if not NA, otherwise... could just be a big case_when call (edit: case_when is probably not right given variable # of args, for loop may be simplest). Optionally create a new variable indicating which one was used.

Besides being more readable than nested ifelse, it would work for dates, which is vexing af if you run into this: http://stackoverflow.com/questions/6668963/how-to-prevent-ifelse-from-turning-date-objects-into-numeric-objects

In fact, have it print a warning if you feed it at least one input vector with class Date, suggesting that they specify force_class = "date.

@sfirke
Copy link
Owner Author

sfirke commented Jul 8, 2016

Like this code I just reviewed:

currentroster <- currentroster %>%
  mutate(recent_rating = ifelse(!is.na(rating2016), rating2016, 
                          ifelse(!is.na(rating2015), rating2015,
                                               rating2014)))

@sfirke
Copy link
Owner Author

sfirke commented Jul 20, 2016

Numeric, logical, character instances are easy. Is it important to retain factor class if all inputs are factors? And how should this work for dates?

Maybe allow the user to specify a class and then the empty result vector will be initialized with that type of NA? Edit: if possible, better to have the function detect the uniform class of the input vectors and do that automatically. Dates -> Dates without user specification.

@sfirke
Copy link
Owner Author

sfirke commented Jul 24, 2016

Not going to have this create a new variable indicating the source vector used for each value. That's a non-critical option, and this is clearest if it always returns a single vector.

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

1 participant