You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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 text was updated successfully, but these errors were encountered:
I believe this is a limitation with the anonymous function you have used rather than an issue with the where() function .
fruit <- c("apple", "banana", "pear", "pinapple", NA); stringr::str_detect(fruit, "a"); [1] TRUE TRUE TRUE TRUE NA
Open to being wrong, though I do believe its a limit of the stringr::str_detect function. Maybe stringr or string has a better alternative?
library(dplyr, warn.conflicts=FALSE)
df<-data.frame(
a= c("x", NA),
b= c(NA, NA),
c= c("y", "y")
)
df|> select(where(~ any(stringr::str_detect(.x, "x"))))
#> Error in `FUN()`:#> ! `where()` must be used with functions that return `TRUE` or `FALSE`.
The
where()
function produces relatively uninformative errors when functions outputNA
values. It would be useful for the user if this was checked.In this example, the function,
~any(str_detect(.x,","))
returnsTRUE
andFALSE
values, but also returnsNA
values, and so the below throws an uninformative error:This can be rectified using
na.rm=T
in theany()
function, as below:I believe that having this throw an error is important, as it draw attention to the
NA
values, 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
NA
values the same asFALSE
The text was updated successfully, but these errors were encountered: