R How to Extract Second Entry of Each List Element (Example Code)

In this R tutorial you’ll learn how to extract the second entry of every list element.

Creation of Example Data

our_example_list <- list(LETTERS[9:6],    # Creating an example list
                5:1,
                letters[1:8],
                c(4, 9, 10, 4))
our_example_list                          # Returning example list
# [[1]]
# [1] "I" "H" "G" "F"
# 
# [[2]]
# [1] 5 4 3 2 1
# 
# [[3]]
# [1] "a" "b" "c" "d" "e" "f" "g" "h"
# 
# [[4]]
# [1]  4  9 10  4

Example: Print Every Second Subelement from Example List

sapply(our_example_list, "[[", 2)         # Applying sapply function in R
# "H" "4" "b" "9"

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.
You need to agree with the terms to proceed

Menu
Top