Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
arnaudgallou opened this issue Aug 26, 2024 · 2 comments
Closed

Comments

@arnaudgallou
Copy link

arnaudgallou commented Aug 26, 2024

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
Copy link
Author

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants