Let's say I have a nested list with many data frames at different levels. I want to extract a flattened list of only data. How can I write this using purrr functions? Should I look at reduce ?
For example, given the data:
s <- list(x = 1:10, data = data.frame(report = LETTERS[1:5], value = rnorm(5, 20, 5)), report = list(A = data.frame(x = 1:3, y = c(2, 4, 6)), B = data.frame(x = 1:3, y = c(3, 6, 9)), z = 4:10, other = data.frame(w = 3:5, color = c("red", "green", "blue"))))
I want to return a function:
list(data = data.frame(report = LETTERS[1:5], value = rnorm(5, 20, 5)), `report$A` = data.frame(x = 1:3, y = c(2, 4, 6)), `report$B` = data.frame(x = 1:3, y = c(3, 6, 9)), `report$other` = data.frame(w = 3:5, color = c("red", "green", "blue")))
I wrote a recursive function:
recursive_keep <- function(.x, .f) { loop <- function(.y) { if(is.list(.y)) { c(keep(.y, .f), flatten(map(discard(.y, .f), loop))) } else if(.f(.y)) { .y } else { NULL } } loop(.x) }
It can be called the following:
recursive_keep(s, is.data.frame)
It seems to be working on this example, but it does not save title information. I want to keep enough information so that I can wrest data from the original object. Maybe this is a simpler question?