Hello!
I am looking for the easiest way to strip my nested list of all non numeric values or to only return those lists (df
) and components that are numeric. I don't mind it being flattened but I want it in a format I can work with as numerics. I am sure there is some way clever way of doing with purrr
but not entirely sure how best to approach it.
library(tidyverse)
library(purrr)
df <- list(a = list(a1 = list(1,2,3),
b1 = list("a","b","c")),
b = list(1,2,3,4,5),
c = list(a1 = list(1,2,3),
b1 = list("a","b","c"),
c1 = list("a","b", c3 = c(1,2,3))
)
)
df2 <- df %>% purrr::flatten()
lapply(df2, function(x){
is.numeric(x)
})
df3 <-
purrr::keep(df2,is.numeric)
df3