Home > database >  How to replace NULL cell by NA in a tibble?
How to replace NULL cell by NA in a tibble?

Time:11-20

With a tibble, it is possible to have NULL cell with lists:

tibble(x = list(1L, NULL), y = 1:2)

which gives us:

# A tibble: 2 x 2
  x             y
  <list>    <int>
1 <int [1]>     1
2 <NULL>        2

that you can explore with View()

enter image description here

How could we replace all NULL cells of a tibble with NA?

The expected output is:

tibble(x = list(1L, NA), y = 1:2)

which produces:

# A tibble: 2 x 2
  x             y
  <list>    <int>
1 <int [1]>     1
2 <lgl [1]>     2

but is in fact:

enter image description here


I have tried:

is.null(df)

but it does not behave like is.na()...


Then I came with:

map(df, function(l) map(l, function(e) if(is.null(e)) NA else e))

But I struggle to make a new tibble with it:

do.call(as_tibble, map(df, function(l) map(l, function(e) if(is.null(e)) NA else e)))

that gives me an error:

Error: Columns 1 and 2 must be named.
Use .name_repair to specify repair.
Run `rlang::last_error()` to see where the error occurred.

CodePudding user response:

A tidyverse approach to achieve your desired result may look like so:

library(purrr)
library(tibble)
library(dplyr)

df <- tibble(x = list(1L, NULL), y = 1:2)

df %>% 
  mutate(across(where(is.list), ~ purrr::modify_if(.x, is.null, ~ NA)))
#> # A tibble: 2 × 2
#>   x             y
#>   <list>    <int>
#> 1 <int [1]>     1
#> 2 <lgl [1]>     2
  • Related