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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# 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 –
- R – Get the Length of a Vector
- How to Create a List in R?
- Convert a List to Vector in R (With Examples)
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.