Get Second Sub Entry of Every List Element in R (Example)


This article illustrates how to extract the second entry of every list element in the R programming language.

Table of contents:

Let’s dive into it!


Creation of Example Data

Consider the exemplifying data below:

my_list <- list(1:3,            # Create list
                c(555, 666))
my_list                         # Print list to console
# [[1]]
# [1] 1 2 3
# [[2]]
# [1] "a" "b" "c" "d" "e" "f"
# [[3]]
# [1] 555 666

Have a look at the previous output of the RStudio console. It reveals that our example data is a list with three list elements.


Example: Extracting Second Sub Element from List Using sapply Function

In this Section, I’ll illustrate how to subset every second entry from each element of our list in R. We can do that by using the sapply function:

sapply(my_list, "[[", 2)        # Using sapply
# "2"   "b"   "666"

Have a look at the previous output: We created a vector with three elements – One vector element for each list element.


Video & Further Resources

Do you need more information on the R code of this tutorial? Then you might watch the following video that I have published on my YouTube channel. I’m explaining the R programming syntax of this page in the video.



In addition, you might want to read the related articles of my website.


You learned in this tutorial how to return only the second sub element from each list element in the R programming language. If you have any further comments and/or questions, don’t hesitate to let me know in the comments below.


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.