# R: Count number of objects in list

## R: Count number of objects in list

length(x)

Get or set the length of vectors (including lists) and factors, and of any other R object for which a method has been defined.

lengths(x)

Get the length of each element of a list or atomic vector (is.atomic) as an integer or numeric vector.

Advice for `R` newcomers like me : beware, the following is a list of a single object :

``````> mylist <- list (1:10)
> length (mylist)
 1
``````

In such a case you are not looking for the length of the list, but of its first element :

``````> length (mylist[])
 10
``````

This is a true list :

``````> mylist <- list(1:10, rnorm(25), letters[1:3])
> length (mylist)
 3
``````

Also, it seems that `R` considers a data.frame as a list :

``````> df <- data.frame (matrix(0, ncol = 30, nrow = 2))
> typeof (df)
 list
``````

In such a case you may be interested in `ncol()` and `nrow()` rather than `length()` :

``````> ncol (df)
 30
> nrow (df)
 2
``````

Though `length()` will also work (but its a trick when your data.frame has only one column) :

``````> length (df)
 30
> length (df[])
 2
``````

#### R: Count number of objects in list

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.

… and just realised someone had already answered this- sorry!