从列表中删除字符为(0)的空元素 [英] Remove empty elements from list with character(0)

查看:316
本文介绍了从列表中删除字符为(0)的空元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从包含零长度对列表的列表中删除空元素,如 character(0)integer(0)等...

How can I remove empty elements from a list that contain zero length pairlist as character(0), integer(0) etc...

list2
# $`hsa:7476`
# [1] "1","2","3"
# 
# $`hsa:656`
# character(0)
#
# $`hsa:7475`
# character(0)
#
# $`hsa:7472`
# character(0)

我不知道该如何对付他们.我的意思是,如果NULL则简单得多.如何删除这些元素,以使hsa:7476仅保留在列表中.

I don't know how to deal with them. I mean if NULL it is much simpler. How can I remove these elements such that just hsa:7476 remains in the list.

推荐答案

通过保留元素长度> 0的索引的另一种选择(我认为更有效):

Another option(I think more efficient) by keeping index where element length > 0 :

l[lapply(l,length)>0] ## you can use sapply,rapply

[[1]]
[1] 1 2 3

[[2]]
[1] "foo"

这篇关于从列表中删除字符为(0)的空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆