Access Index Names of List Using lapply Function in R (Example)

 

This post explains how to work with list indices within the FUN argument of the lapply function in R.

The article will contain one example for the application of the lapply function. More precisely, the article looks as follows:

Let’s take a look at some R codes in action…

 

Creation of Example Data

We’ll use the following data as basement for this R tutorial:

my_list <- list(a = 1:5,        # Create example list
                b = 9:5,
                c = LETTERS[1:3])
my_list                         # Print example list
# $a
# [1] 1 2 3 4 5
# 
# $b
# [1] 9 8 7 6 5
# 
# $c
# [1] "A" "B" "C"

As you can see based on the previous output of the RStudio console, our example list contains three list elements with the names a, b, and c. Each of these list elements contains a vector object.

 

Example: Access lapply() Indices Inside FUN Using seq_along() & function()

The R programming code below explains how to handle list index names when using the lapply function in R.

For this, we have to apply the seq_along function to our list, and we have to specify a user-defined function, in which we access the list index names and values.

Within this function, we can specify basically whatever we want. In the following example, we are telling the function to return a sentence containing the list element name and the corresponding values to each list element.

Have a look at the following R code and its output:

lapply(seq_along(my_list), 
       function(i) paste("The list element No.",
                         i,
                         "with the name",
                         names(my_list)[[i]],
                         "contains the values",
                         paste(my_list[[i]], collapse = ", ")))
# [[1]]
# [1] "The list element No. 1 with the name a contains the values 1, 2, 3, 4, 5"
# 
# [[2]]
# [1] "The list element No. 2 with the name b contains the values 9, 8, 7, 6, 5"
# 
# [[3]]
# [1] "The list element No. 3 with the name c contains the values A, B, C"

As you can see, we have created a list output where each list element contains a sentence corresponding to our input list.

 

Video & Further Resources

Have a look at the following video instruction of my YouTube channel. In the video, I’m explaining the R syntax of this article in RStudio:

 

 

In addition, you might have a look at the other R programming articles of my homepage. I have published numerous tutorials about functions in R already.

 

In this tutorial, I have shown how to access and preserve list index names within the lapply command in the R programming language. Tell me about it in the comments, in case you have any additional questions.

 

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.

Top