删除列表的空白子项

Rya*_*win 0 r

嗨,我想删除列表中的空白子项:

l <- list(c(1:3), c(1,"",3))
Run Code Online (Sandbox Code Playgroud)

输出应该是这样的

[[1]]
[1] 1 2 3
[[2]]
[1] 1 3
Run Code Online (Sandbox Code Playgroud)

我试过以下但没有成功:

l[lapply(l, function(x) x != "")]
Run Code Online (Sandbox Code Playgroud)

而我得到的错误:

Error in l[lapply(l, function(x) x != "")] : 
 invalid subscript type 'list'
Run Code Online (Sandbox Code Playgroud)

看似简单,但我还没有在SO上找到解决方案.

tal*_*lat 5

循环遍历列表,并将每个向量子集化以删除空白元素:

lapply(l, function(x) as.numeric(x[x!= ""]))
#[[1]]
#[1] 1 2 3
#
#[[2]]
#[1] 1 3
Run Code Online (Sandbox Code Playgroud)

我补充说as.numeric这似乎合情合理.