Search code examples
rlisttibble

R: How to remove nested lists from columns?


I am looking to remove nested list columns from a tibble with lists. For example in the list below I want to remove "affected_rows" without specifically referencing the column name. How would i best approach this?

my_list <- list(
  a =
    tibble(
      code = c("ax","yz"),
      affected_rows =
        c(list(1:10),list(200))
    ),
  b = 
    tibble(
      workid = c("123","456"),
      sheet = c("sheet1", "sheet2")
    )
)

Solution

  • Another possible solution:

    library(tidyverse)
      
    map(my_list, ~ .x %>% mutate(across(where(is.list), as.null))) 
    
    #> $a
    #> # A tibble: 2 × 1
    #>   code 
    #>   <chr>
    #> 1 ax   
    #> 2 yz   
    #> 
    #> $b
    #> # A tibble: 2 × 2
    #>   workid sheet 
    #>   <chr>  <chr> 
    #> 1 123    sheet1
    #> 2 456    sheet2