Select First Element of Nested List in R (2 Examples)
In this article, I’ll show how to extract the top of each list element in a nested list in the R programming language.
The tutorial will contain two examples for the extraction of list elements. More precisely, the content of the page is structured as follows:
It’s time to dive into the R code.
Introducing Example Data
As a first step, we have to construct some data that we can use in the examples below.
my_list <- list(list(1, 5, 9, 1), # Example list list("a", "b", "c"), list(5, 7, 1)) my_list # Print list # [[1]] # [[1]][[1]] # [1] 1 # # [[1]][[2]] # [1] 5 # # [[1]][[3]] # [1] 9 # # [[1]][[4]] # [1] 1 # # # [[2]] # [[2]][[1]] # [1] "a" # # [[2]][[2]] # [1] "b" # # [[2]][[3]] # [1] "c" # # # [[3]] # [[3]][[1]] # [1] 5 # # [[3]][[2]] # [1] 7 # # [[3]][[3]] # [1] 1 |
my_list <- list(list(1, 5, 9, 1), # Example list list("a", "b", "c"), list(5, 7, 1)) my_list # Print list # [[1]] # [[1]][[1]] # [1] 1 # # [[1]][[2]] # [1] 5 # # [[1]][[3]] # [1] 9 # # [[1]][[4]] # [1] 1 # # # [[2]] # [[2]][[1]] # [1] "a" # # [[2]][[2]] # [1] "b" # # [[2]][[3]] # [1] "c" # # # [[3]] # [[3]][[1]] # [1] 5 # # [[3]][[2]] # [1] 7 # # [[3]][[3]] # [1] 1
As you can see based on the previous output of the RStudio console, our exemplifying data is a nested list containing three lists.
Example 1: Extract First Element of Nested List Using lapply Function
Example 1 shows how to use the lapply function to return the first element of each list.
lapply(my_list, `[[`, 1) # Use lapply function # [[1]] # [1] 1 # # [[2]] # [1] "a" # # [[3]] # [1] 5 |
lapply(my_list, `[[`, 1) # Use lapply function # [[1]] # [1] 1 # # [[2]] # [1] "a" # # [[3]] # [1] 5
The previous output shows the result of our R code: A list containing three elements.
Example 2: Extract First Element of Nested List Using purrr Package
This Section explains how to use the purrr package. We first need to install and load the purrr package:
install.packages("purrr") # Install & load purrr package library("purrr") |
install.packages("purrr") # Install & load purrr package library("purrr")
Now, we can use the map function provided by purrr:
map(my_list, 1) # Use map function # [[1]] # [1] 1 # # [[2]] # [1] "a" # # [[3]] # [1] 5 |
map(my_list, 1) # Use map function # [[1]] # [1] 1 # # [[2]] # [1] "a" # # [[3]] # [1] 5
The output is the same as in Example 1.
Video & Further Resources
Do you need further info on the R programming syntax of this article? Then you may watch the following video of my YouTube channel. I’m explaining the topics of this tutorial in the video:
The YouTube video will be added soon.
In addition, you might have a look at the other articles on my website:
- Split Data Frame into List of Data Frames Based On ID Column
- Convert Vector to List in R
- Add New Elements to a List
- Create List of Data Frames
- The R Programming Language
Summary: This article explained how to return the upper element of each list object within a nested list in R programming. Let me know in the comments, in case you have additional questions.
Statistics Globe Newsletter