
Generate list or list-column with NULLs replaced with NAs
Source:R/gen_na_listcol.R
gen_na_listcol.Rd
This function takes a list and replaces all NULL
values with NA
. It is useful for working with list-columns in a data frame.
Details
Parallelization is supported via purrr::in_parallel()
.
Examples
library(dplyr, warn.conflicts = FALSE)
a <-
mtcars %>%
select(cyl, vs, am) %>%
slice(1:6) %>%
as_tibble() %>%
mutate(listcol = list(NULL, "b", "c", "d", "e", "f"))
glimpse(a)
#> Rows: 6
#> Columns: 4
#> $ cyl <dbl> 6, 6, 4, 6, 8, 6
#> $ vs <dbl> 0, 0, 1, 1, 0, 1
#> $ am <dbl> 1, 1, 1, 0, 0, 0
#> $ listcol <list> <NULL>, "b", "c", "d", "e", "f"
b <-
a %>%
mutate(across(starts_with("listcol"), gen_na_listcol))
glimpse(b)
#> Rows: 6
#> Columns: 4
#> $ cyl <dbl> 6, 6, 4, 6, 8, 6
#> $ vs <dbl> 0, 0, 1, 1, 0, 1
#> $ am <dbl> 1, 1, 1, 0, 0, 0
#> $ listcol <list> NA, "b", "c", "d", "e", "f"