Skip to Content

Get Length of a List in R (With Examples)

A list in an in-built data structure in R that is generally used to store one-dimensional data. A list can store values of different data types and even other lists as its elements. In this tutorial, we will look at how to get the length of a list in R with the help of some examples.

How to get the length of a list in R?

You can use the built-in length() function in R to get the length of a list. Pass the list for which you want to get the length as an argument. The following is the syntax –

# get list length
length(x)

It returns the length of the list. If, on the other hand, you want to compute the length of each element in the list, use the lengths() (ends with an s) function instead.

Examples

Let’s look at some examples of using the above syntax in R.

Length of a list of numbers

First, let’s get the length of a simple list of numbers. For this, we will first create a list of numbers using the list() function and then use the length() function to get its length.

# create a list
ls <- list(1, 2, 3, 4)
# get list length
print(length(ls))

Output:

[1] 4

We get the length as 4. You can see that the length of the list denotes the number of elements in the list.

Length of a list with named elements

Let’s now see what happens if we use the length() function on a list containing named elements.

# create a list with named elements
ls <- list("a"=1, "b"=2, "c"=3, "d"=4)
# get list length
print(length(ls))

Output:

[1] 4

You can see that we get the length of the list as 4. Since the length of the list gives us the number of elements in the list, it doesn’t matter whether the elements are named or not when computing the length.

Length of a list of lists

What happens if you use the length() function on a list with elements as lists? Let’s find out.

# list with elements as lists
ls <- list(list(1, 2, 3), list(4, 5), list(6, 7, 8, 9))
# get list length
print(length(ls))

Output:

[1] 3

Here the list ls is a list of three lists. The length function returns 3, which is the number of elements (objects) in the list.

Length of each element in a list

If you want to get the length of each element in a list (instead of the total length of the list), you can use the lengths() function (Note that this function name ends with an s). It returns a vector containing the length of each element in the list.

Let’s look at an example.

# create a list
ls <- list(1, 2, 3, 4)
# get length of each element in list
print(lengths(ls))

Output:

[1] 1 1 1 1

We get the length of each element as 1 since all the values in this list are scaler values.

Let’s use this function on a list of lists.

# list with elements as lists
ls <- list(list(1, 2, 3), list(4, 5), list(6, 7, 8, 9))
# get length of each element in list
print(lengths(ls))

Output:

[1] 3 2 4

We get a vector with the length of each element (a list in this example). You can see that the first, second, and third elements have lengths 3, 2, and 4 respectively.

What if the elements in the list are named? Let’s find out.

# list with elements as lists
ls <- list("a"=list(1, 2, 3), "b"=list(4, 5), "c"=list(6, 7, 8, 9))
# get length of each element in list
print(lengths(ls))

Output:

a b c 
3 2 4 

We get the length for each named element in the above list.

In this tutorial, we looked at how to get the length of a list using the length() function and how to get the length of each element in a list using the lengths() function in R.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.