Making the code more generic with `safe_ifelse`

The idea is to leverage `Rcpp`'s ifelse, but beware that it is currently broken (it does not handle NA correctly). So you might want to keep your version until it is available to you (if you don't build Rcpp out of github). 

I submitted a PR to Rcpp as well. https://github.com/RcppCore/Rcpp/pull/150