尽管标题看起来很简单,但我还没有在StackOverflow上找到答案.我的问题是:我有一些元素的列表.列表中的每个元素还包含一个填充了一些字符串的列表.列表内的列表包含空('')的值,而不是null.我想删除该列表中空的行.怎么办呢?我猜lapply会做的伎俩,但我不知道如何在那里使用它.
将不胜感激!非常感谢!
编辑:所以我的输出看起来像这样
$'1'
1 text1
2 text2
3
4 text3
5 text4
$'2'
1 text1
2
3 text2
4
5 text3
Run Code Online (Sandbox Code Playgroud)
我的目标是删除那些空的行,使它看起来像这样:
$'1'
1 text1
2 text2
3 text3
4 text4
$'2'
1 text1
2 text2
3 text3
Run Code Online (Sandbox Code Playgroud)
小智 7
你是对的,你可以lapply在这里使用.
test <- list(c("text1", "text2", "", "text3", "text4"),
c("text1", "", "text2", "", "text3"))
lapply(1:length(test), function(x) test[[x]][test[[x]] != ""])
[[1]]
[1] "text1" "text2" "text3" "text4"
[[2]]
[1] "text1" "text2" "text3"
Run Code Online (Sandbox Code Playgroud)