tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.78k stars 2.12k forks source link

`if_else()` coerces `NA` as `list(NULL)` when the `false` argument is a list #7076

Closed arnaudgallou closed 3 months ago

arnaudgallou commented 3 months ago

See this example:

dplyr::if_else(
  TRUE,
  NA,
  list("foo")
)
#> [[1]]
#> NULL

dplyr::if_else(
  TRUE,
  NA,
  "foo"
)
#> [1] NA

Created on 2024-08-27 with reprex v2.1.0

Is this the desired behavior? I'd expect if_else() to return list(NA) in the first case.

arnaudgallou commented 3 months ago

In the end, that might be the desired behavior but maybe that could be mentioned in if_else() (and possibly in vctrs::vec_cast_common()) that NA will be changed to NULL in that case.

DavisVaughan commented 3 months ago

It is expected behavior. The "missing" type for a list is list(NULL), so NA is cast to that when we compute the common type.

There's really no good place to put docs for this, as it is quite specific and not that common, but I'll add an example to vec_init() with a list to show that the NA element for a list is NULL