我想从列表列表中删除具有character(0)
的列表元素(未命名)。以下是示例输出。请建议我该如何继续。
[[984]]
character(0)
[[985]]
character(0)
[[986]]
[1] "Mazhar" "LFG" "Comments" "coding" "contribution" "quality"
[[987]]
[1] "Comments" "good"
[[988]]
[1] "Comments" "Quality" "Good" "team"
[[989]]
[1] "experience has vast" "Comments" "effective" "experience"
[5] "technologies" "lead" "team members" "members"
[9] "team" "good"
[[990]]
[1] "bring the process" "processes" "bring" "guidelines" "standards"
[6] "handling" "Comments" "project"
[[991]]
[1] "AEM" "AEM though new"
[[992]]
[1] "communication"
[[993]]
character(0)
[[994]]
[1] "day" "work"
答案 0 :(得分:1)
要消除character(0)
的列表元素,我们可以否定identical()
。
list[!sapply(list, identical, character(0))]
其中list
是您的列表。
可重复的例子:
x <- list(character(0), 1, 2, character(0))
x[!sapply(x, identical, character(0))]
# [[1]]
# [1] 1
#
# [[2]]
# [1] 2
答案 1 :(得分:1)
我们可以使用lengths
来创建逻辑向量。具有length
的元素的character(0)
将为0.因此,进行相等比较会获得用于对list
进行子集化的逻辑向量
out <- lst[lengths(lst)!=0]
或另一个选项是Filter
Filter(length, lst)
lst <- list(character(0), letters[1:3], character(0), letters[1:2])