Split list of list in a list

Suppose I have list inside which I have again list of different length.

animals <- list(mammals = list("cat", "dog", "bear"), amphibians = list("frog"))

If the insider list length is greater than 1, then it will split to vector and at the end I will have a list of different animal vector.

I am attaching here my desired output which I have hard coded. I am looking for some function which will do that for me.

I would try


but its possible I haven't understood your target.
perhaps you could dput(all_list)

Does this produce the result you want?

animals <- list(mammals = list("cat", "dog", "bear"), amphibians = list("frog"))
animals %>% map_dfr(~tibble(species = as.character(.)), .id = "family")
#> # A tibble: 4 x 2
#>   family     species
#>   <chr>      <chr>  
#> 1 mammals    cat    
#> 2 mammals    dog    
#> 3 mammals    bear   
#> 4 amphibians frog

Created on 2021-06-02 by the reprex package (v1.0.0)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.