简体   繁体   English

如何计算列表中的总元素

[英]How to calculate total elements in the list

I have a list. 我有一个清单。

x <- list(1:3, 4:5, 6:9)

> x
#[[1]]
#[1] 1 2 3

#[[2]]
#[1] 4 5

#[[3]]
#[1] 6 7 8 9

I need to find the total elements in the list which is 9 in this case. 我需要在列表中找到总数为9的元素。 By using length(x) i get output as 3. I am sure there must be something simple solution to this. 通过使用length(x)我得到的输出为3。我确信对此必须有一些简单的解决方案。 Any help would be appreciated. 任何帮助,将不胜感激。

You can use a combination of length and unlist . 您可以结合使用lengthunlist

x <- list(1:3, 4:5, 6:9)
length(unlist(x))
# [1] 9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM