I have a list of a mix of dataframes, tibble and empty list. How do I remove the tibble and empty list before applying bind_rows
to append the rest of the data frames?
I tried using the delete.NULLs
function but there was an error:
Error: could not find function "delete.NULLs"
We can use discard
library(purrr)
discard( lst1, ~is.vector(.x) || is.null(.x)|is_tibble(.x) )
EDIT: From @ArtemSokolov's comments
Or from base R
out <- Filter(function(x) !(is.vector(x) | is.null(x) |is_tibble(x)), lst1)
out
#[[1]]
# col1
#1 1
#2 2
#3 3
#[[2]]
# A B
#1 1 2
#2 2 3
#3 3 4
#4 4 5
#5 5 6
The delete.NULLs
function is not found in base R
. But, it could be created with a combination of is.null
and negate ( !
).
lst1 <- list(data.frame(col1 = 1:3), NULL, tibble(col1 = 1:5,
col2 = 2:6), NA, data.frame(A = 1:5, B = 2:6))
Using @akrun data:
lst1[unlist(lapply(lst1, function(x) !(is.null(x) | is_tibble(x))))]
Regarding your question about NA
:
lst1 <- list(data.frame(col1 = 1:3), NULL, tibble(col1 = 1:5,
col2 = 2:6), data.frame(A = 1:5, B = 2:6), NA)
lst <-lst1[unlist(lapply(lst1, function(x) !(is.null(x) | is_tibble(x))))]
lst<-lst[!is.na(lst)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.