list 计算列表中对象的数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1740524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count number of objects in list
提问by Karl
R function that will return the number of items in a list?
R函数将返回列表中的项目数?
回答by Joey
Get or set the length of vectors (including lists) and factors, and of any other R object for which a method has been defined.
获取或设置向量(包括列表)和因子的长度,以及已为其定义方法的任何其他 R 对象的长度。
Get the length of each element of a list or atomic vector (is.atomic) as an integer or numeric vector.
获取列表或原子向量 (is.atomic) 的每个元素的长度作为整数或数字向量。
回答by Skippy le Grand Gourou
Advice for R
newcomers like me?: beware, the following is a list of a single object?:
给R
像我这样的新手的建议?:当心,以下是单个对象的列表?:
> mylist <- list (1:10)
> length (mylist)
[1] 1
In such a case you are not looking for the length of the list, but of its first element?:
在这种情况下,您不是在寻找列表的长度,而是在寻找它的第一个元素的长度?:
> length (mylist[[1]])
[1] 10
This is a "true" list?:
这是一个“真实”列表?:
> mylist <- list(1:10, rnorm(25), letters[1:3])
> length (mylist)
[1] 3
Also, it seems that R
considers a data.frame as a list?:
另外,似乎将R
data.frame 视为列表?:
> df <- data.frame (matrix(0, ncol = 30, nrow = 2))
> typeof (df)
[1] "list"
In such a case you may be interested in ncol()
and nrow()
rather than length()
?:
在这种情况下,您可能对ncol()
andnrow()
而不是length()
?感兴趣:
> ncol (df)
[1] 30
> nrow (df)
[1] 2
Though length()
will also work (but it's a trick when your data.frame has only one column)?:
虽然length()
也可以工作(但是当你的 data.frame 只有一列时这是一个技巧)?:
> length (df)
[1] 30
> length (df[[1]])
[1] 2
回答by anon
I spent ages trying to figure this out but it is simple! You can use length(·)
. length(mylist)
will tell you the number of objects mylist
contains.
我花了很长时间试图弄清楚这一点,但这很简单!您可以使用length(·)
. length(mylist)
会告诉你mylist
包含的对象数量。
... and just realised someone had already answered this- sorry!
......刚刚意识到有人已经回答了这个 - 对不起!
回答by Jeff Kraus
Let's create an empty list (not required, but good to know):
让我们创建一个空列表(不是必需的,但很高兴知道):
> mylist <- vector(mode="list")
Let's put some stuff in it - 3 components/indexes/tags (whatever you want to call it) each with differing amounts of elements:
让我们在其中放一些东西 - 3 个组件/索引/标签(无论你想怎么称呼它),每个组件/索引/标签都有不同数量的元素:
> mylist <- list(record1=c(1:10),record2=c(1:5),record3=c(1:2))
If you are interested in just the number of components in a list use:
如果您只对列表中的组件数量感兴趣,请使用:
> length(mylist)
[1] 3
If you are interested in the length of elements in a specific component of a list use: (both reference the same component here)
如果您对列表的特定组件中元素的长度感兴趣,请使用:(两者都在此处引用相同的组件)
length(mylist[[1]])
[1] 10
length(mylist[["record1"]]
[1] 10
If you are interested in the length of all elements in all components of the list use:
如果您对列表中所有组件中所有元素的长度感兴趣,请使用:
> sum(sapply(mylist,length))
[1] 17
回答by user6062513
You can also use unlist()
, which is often useful for handling lists:
您还可以使用unlist()
,这通常对处理列表很有用:
> mylist <- list(A = c(1:3), B = c(4:6), C = c(7:9))
> mylist
$A
[1] 1 2 3
$B
[1] 4 5 6
$C
[1] 7 8 9
> unlist(mylist)
A1 A2 A3 B1 B2 B3 C1 C2 C3
1 2 3 4 5 6 7 8 9
> length(unlist(mylist))
[1] 9
unlist() is a simple way of executing other functions on lists as well, such as:
unlist() 也是在列表上执行其他函数的一种简单方法,例如:
> sum(mylist)
Error in sum(mylist) : invalid 'type' (list) of argument
> sum(unlist(mylist))
[1] 45