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

如何在使用bind_rows()之前删除列表中的空数据帧?,r,R,我有一个数据帧、TIBLE和空列表的混合列表。在应用bind_rows附加其余的数据帧之前,如何删除tibble和空列表 我尝试使用delete.NULLs函数,但出现错误: 错误:找不到函数“delete.NULLs” 我们可以使用discard library(purrr) discard( lst1, ~is.vector(.x) || is.null(.x)|is_tibble(.x) ) 编辑:来自@ArtemSokolov的评论 或从base R out <- Filte

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

我尝试使用
delete.NULLs
函数,但出现错误:

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


我们可以使用
discard

library(purrr)
discard( lst1, ~is.vector(.x) || is.null(.x)|is_tibble(.x) )
编辑:来自@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
使用@akrun数据:

lst1[unlist(lapply(lst1, function(x) !(is.null(x) | is_tibble(x))))]
关于您关于
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)]

lst1解决了na和tibble问题,但我意识到在我的列表中有一个带有数据“na”的“逻辑”类型。我怎样才能删除它?@Jein最好给出一个小的可重复播放的例子。您的意思是列表值仅为
NA
@akrun:您不需要
map\u lgl
内部丢弃。直接使用您的谓词公式作为第二个参数:
discard(lst1,~is.null(.x)| is_tible(.x))
在什么情况下,您的代码与akrun的代码会有所不同?没有,这只是解决问题的另一种方法。这解决了na和TIBLE问题,但我意识到在我的列表中有一个带有数据“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)]