Skip to content

where() with NA values #236

@MyKo101

Description

@MyKo101

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions