The where() function produces relatively uninformative errors when functions output NA values. It would be useful for the user if this was checked.
In this example, the function, ~any(str_detect(.x,",")) returns TRUE and FALSE values, but also returns NA values, and so the below throws an uninformative error:
gss_cat %>%
select(where(~any(str_detect(.x,","))))
Error: `where()` must be used with functions that return `TRUE` or `FALSE`.
This can be rectified using na.rm=T in the any() function, as below:
gss_cat %>%
select(where(~any(str_detect(.x,","),na.rm=T)))
I believe that having this throw an error is important, as it draw attention to the NA values, but since all the values are logical, the error should be more informative.
This is also in contrast to the select_if() version of this call:
gss_cat %>%
select_if(~any(str_detect(.,",")))
which treats NA values the same as FALSE
The
where()function produces relatively uninformative errors when functions outputNAvalues. It would be useful for the user if this was checked.In this example, the function,
~any(str_detect(.x,","))returnsTRUEandFALSEvalues, but also returnsNAvalues, and so the below throws an uninformative error:This can be rectified using
na.rm=Tin theany()function, as below:I believe that having this throw an error is important, as it draw attention to the
NAvalues, but since all the values arelogical, the error should be more informative.This is also in contrast to the
select_if()version of this call:which treats
NAvalues the same asFALSE