如何在使用 bind_rows() 之前删除列表中的空数据帧?

Jan*_*ane 3 r

我有一个混合数据帧、tibble 和空列表的列表。如何在申请bind_rows附加其余数据帧之前删除 tibble 和空列表?

我尝试使用该delete.NULLs功能,但出现错误:

错误:找不到函数“delete.NULLs”

akr*_*run 5

我们可以用 discard

library(purrr)
discard( lst1, ~is.vector(.x) || is.null(.x)|is_tibble(.x) )
Run Code Online (Sandbox Code Playgroud)

编辑:来自@ArtemSokolov 的评论


或者从 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
Run Code Online (Sandbox Code Playgroud)

delete.NULLs找不到该函数base R。但是,它可以用is.null和 否定 ( !)的组合来创建。

数据

lst1 <- list(data.frame(col1 = 1:3), NULL, tibble(col1 = 1:5, 
             col2 = 2:6), NA, data.frame(A = 1:5, B = 2:6))
Run Code Online (Sandbox Code Playgroud)


Loc*_*ris 1

使用@akrun数据:

lst1[unlist(lapply(lst1, function(x) !(is.null(x) | is_tibble(x))))]
Run Code Online (Sandbox Code Playgroud)

关于您的问题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)]
Run Code Online (Sandbox Code Playgroud)