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 letters[1:6], 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.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
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.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.






