Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 列表中空位置的索引_R_List - Fatal编程技术网

R 列表中空位置的索引

R 列表中空位置的索引,r,list,R,List,我的清单如下: l <- list(c(1,2,3), integer(), c(4,5), integer(), 7) c(2, 4) 列表l没有元素的位置。我可以通过以下方式实现这一点: which(unlist(lapply(c(1:(length(l))), function(x) { length(l[[x]]) })) == 0) 甚至是: as.vector(which(unlist(lapply(l, length)) == 0)) 但我觉得我有点复杂化了。有没有一

我的清单如下:

l <- list(c(1,2,3), integer(), c(4,5), integer(), 7)
c(2, 4)
列表
l
没有元素的位置。我可以通过以下方式实现这一点:

which(unlist(lapply(c(1:(length(l))), function(x) { length(l[[x]]) })) == 0)
甚至是:

as.vector(which(unlist(lapply(l, length)) == 0))

但我觉得我有点复杂化了。有没有一种更简单的方法,不用使用这么多函数,就可以获得此列表中空位置的索引?

您应该使用
length
函数,该函数给出列表中所有元素的长度:

which(lengths(l)==0)
#[1] 2 4
length
函数有点“新”(如果我没有记错的话,可以从R3.2.0开始使用)。如果您使用的是较旧的R版本,这也可以使用(但速度较慢):


其中(长度(l)==0)
。棒极了。请回答。
which(sapply(l,length)==0)