I have just been learning how to use case_when() to replace chains of ifelse() statements. This is a very convenient function. (Thanks, Hadley, or whoever wrote this function.) I have learned that when I have NA as the right hand side of a formula, I have to "type" the NA as NA_real_ or NA_integer_ or whatever is appropriate, since plain NA is interpreted as logical and breaks things because of class inconsistency.
But I am now working with several large data sets containing (mostly) integer columns as the data are imported to R. Now, when I run case_when using these datasets, I have to explicitly cast all of the RHS values as integer as (it seems) that the RHS of the formula creates them as double, and the class inconsistency abends everything with a class conflict.
(The documentation says: " A vector of length 1 or
n , matching the length of the logical input or output vectors, with the type (and attributes) of the first RHS." But my experience says that if I cast the first RHS as.integer, I have to cast all the other RHS as integer, too, or the function breaks because of class inconsistency.)
So, is there a way that I can call case_when() to output integer values by default, and in particular, when my first formula RHS is a literal integer?
Thanks for any help with this.