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 |
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" |
sapply(our_example_list, "[[", 2) # Applying sapply function in R # "H" "4" "b" "9"