Home > Blockchain >  Remove empty lists from a tibble in R
Remove empty lists from a tibble in R

Time:02-16

I am trying to remove any list from my tibble that has "<chr [0]>"

library(tidyverse)

df <- tibble(x = 1:3, y = list(as.character()), 
             z=list(as.character("ATC"),as.character("TAC"), as.character()))
df
#> # A tibble: 3 × 3
#>       x y         z        
#>   <int> <list>    <list>   
#> 1     1 <chr [0]> <chr [1]>
#> 2     2 <chr [0]> <chr [1]>
#> 3     3 <chr [0]> <chr [0]>

Created on 2022-02-15 by the reprex package (v2.0.1)

I want my tibble to look like this

#> # A tibble: 3 × 3
#>       x  z        
#>    <int> <list>   
#> 1     1 <chr [1]>
#> 2     2 <chr [1]>
#> 3     3    NA

any help is appreciated

CodePudding user response:

You can do:

df %>%
  select(where(~!all(lengths(.) == 0))) %>%
  mutate(z = lapply(z, function(x) ifelse(length(x) == 0, NA, x)))

# A tibble: 3 x 2
      x z        
  <int> <list>   
1     1 <chr [1]>
2     2 <chr [1]>
3     3 <lgl [1]>

Note, in your z column you can‘t have list elemtents for row 1 and 2 and a direct logical value NA. The whole column needs to be a list.

If all elements of z have only one element, you can add another line of code with mutate(z = unlist(z)).

CodePudding user response:

A two-step way using base R:

df <- tibble(x = 1:3, y = list(as.character()), 
             z=list(as.character("ATC"),as.character("TAC"), as.character()))


df <- df[apply(df, 2, function(x) any(lapply(x, length) > 0))] #Remove empty columns
df[apply(df, 2, function(x) lapply(x, length) == 0)] <- NA #Replace empty lists with NA

df
# A tibble: 3 x 2
      x z        
  <int> <list>   
1     1 <chr [1]>
2     2 <chr [1]>
3     3 <NULL> 
  • Related