How to Remove a Level of Lists from a List of Lists

How to remove a level of lists from a list of lists

I think this does the trick

new.pp <- unlist(pp,recursive=FALSE)

how to remove elements in list of lists of nested lists?

This is the right scenario to use modify_depth, which functions as a shortcut for chains of modify to access deep nested lists. modify has an advantage over map in this problem because it will preserve the type of the input instead of coercing everything to lists, which may be relevant if you have vector elements of your list structure.

Using your given input (with a p3 element inside rather than on the same level as p2), the dataframe elements at the second and third levels are discarded as below. In order to search all levels of the nested list, we can set up a while loop to iterate through the levels, discarding dataframes as we go. We need .ragged = TRUE to deal with errors with list depth. This version searches bottom up but you could change it to search top down as well.

library(tidyverse)
x <- list(
p1 = list(type = "A", score = list(c1 = 10, c2 = 8, c3 = data.frame(a = 1, b = 3, c = 5))),
p2 = list(
type = "B", score = list(c1 = 9, c2 = 9, c3 = data.frame(a = 2, b = 2)),
p3 = list(type = "B", score = list(c1 = 9, c2 = 7, c3 = data.frame(a = 2, b = 2)))
)
)

remove_dataframes <- function(input_list) {
current_list <- input_list
current_depth <- vec_depth(current_list)
# current_depth <- max_depth
while (current_depth > 1) {
current_list <- modify_depth(
.x = current_list,
.depth = current_depth,
.f = ~ discard(., is.data.frame),
.ragged = TRUE
)
current_depth <- current_depth - 1
}
return(current_list)
}

x %>%
remove_dataframes %>%
glimpse
#> List of 2
#> $ p1:List of 2
#> ..$ type : chr "A"
#> ..$ score:List of 2
#> .. ..$ c1: num 10
#> .. ..$ c2: num 8
#> $ p2:List of 3
#> ..$ type : chr "B"
#> ..$ score:List of 2
#> .. ..$ c1: num 9
#> .. ..$ c2: num 9
#> ..$ p3 :List of 2
#> .. ..$ type : chr "B"
#> .. ..$ score:List of 2

Created on 2019-02-20 by the reprex package (v0.2.1)

Removing NA from list of lists in R in function

Use rapply:

rapply(L.miss, na.omit, how = "replace")


Related Topics



Leave a reply



Submit