Count Number of List Elements in R (2 Examples)


On this page, I’ll show how to count the number of list elements and the number of objects within each list element in the R programming language.

The tutorial will contain two examples for the counting in lists. To be more specific, the page is structured as follows:

Let’s jump right to the examples.


Example Data

First, we need to create an example list in R:

my_list <- list(L1 = "AAA",                      # Create example list
                L2 = 1:7,
                L3 = c("Hello", "What's up?"))
my_list                                          # Print example list
# $L1
# [1] "AAA"
# $L2
# [1] 1 2 3 4 5 6 7
# $L3
# [1] "Hello"      "What's up?"

As you can see based on the output of the RStudio console, our example list consists of three list elements and each list elements contains of a different number of list objects.

However, in the following I’ll show how to get the amount of list elements based on very simple functions of the R programming language. So keep on reading!


Example 1: Count Number of Elements in List

Example 1 explains how to return the amount of list elements in a list. Have a look at the following R code:

length(my_list)                                  # Count number of list elements
# 3

In the previous R syntax we applied the length function to our example list and the length function returned the value 3 to the RStudio console. This value reflects the amount of list elements in our list.


Example 2: Count Number of Objects within Each List Element

Example 2 illustrates how to count the number of data objects within each of our list elements. In this example, we are using the lengths() function (note that I’m speaking about the lengths function with an s at the end; not the length function without an s at the end that we have used in Example 1). Let’s have a look at the function in action:

lengths(my_list)                                 # Count within each list element
# L1 L2 L3 
#  1  7  2

The lengths command returned a named vector, whereby each element of the vector is representing the number of objects within the related list element. The list element L1 consists of 1 data object, the list element L2 consists of 7 data object, and the list element L3 consists of 2 data object.


Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which illustrates the R syntax of this tutorial. You can find the video below:



Furthermore, you might have a look at the other tutorials of my website. I have released several articles about the handling of lists in the R programming language already:


In this article, I explained how to get the amount of observations in lists in the R programming language. If you have further questions, let me know in the comments section.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.