Skip to content

dplyr::if_else() fails when condition is a labelled logical vector #6678

Closed

Description

@salim-b

Starting with dplyr 1.1.0, dplyr::if_else() fails with a meaningless error message when condition is a labelled logical vector:

d <-
    mtcars |>
    dplyr::mutate(mileage = mpg > 20) |>
    labelled::set_variable_labels(mileage = "mileage dummy")

dplyr::if_else(d$mileage,
               "guzzler",
               "moderate")
#> Error in `dplyr::if_else()`:
#> ! `condition` must be a vector with type <logical>.
#> Instead, it has type <logical>.

#> Backtrace:
#>     ▆
#>  1. └─dplyr::if_else(d$mileage, "guzzler", "moderate")
#>  2.   └─vctrs::vec_assert(x = condition, ptype = logical(), arg = "condition")
#>  3.     └─rlang::abort(...)

Created on 2023-02-02 with reprex v2.0.2

Explicitly providing the correct prototype gives the same result:

dplyr::if_else(d$mileage,
               "guzzler",
               "moderate",
               ptype = vctrs::vec_ptype(d$mileage))
#> Error in `dplyr::if_else()`:
#> ! `condition` must be a vector with type <logical>.
#> Instead, it has type <logical>.

#> Backtrace:
#>     ▆
#>  1. └─dplyr::if_else(d$mileage, "guzzler", "moderate", ptype = vctrs::vec_ptype(d$mileage))
#>  2.   └─vctrs::vec_assert(x = condition, ptype = logical(), arg = "condition")
#>  3.     └─rlang::abort(...)

Activity

  1. changed the title [-][`dplyr::if_else()`](https://dplyr.tidyverse.org/reference/if_else.html) fails when `condition` is a **labelled** logical vector[/-] [+]`dplyr::if_else()` fails when `condition` is a **labelled** logical vector[/+] on Feb 2, 2023
  2. changed the title [-]`dplyr::if_else()` fails when `condition` is a **labelled** logical vector[/-] [+]`dplyr::if_else()` fails when `condition` is a labelled logical vector[/+] on Feb 2, 2023
  3. added this to the 1.1.1 milestone on Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions