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
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
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")
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
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
8 Comments. Leave new
Hi! I have a question regarding extracting specific elements from lists.
I am working on a project at university and we have a large dataframe given… I have a list of strings reprecenting moves in chess. I would like to select the uneven numbers of each list in order to differentiate which moves are made by white and which black.
moves_char <- as.character(droplevels(daten$moves))
moves_split <- sapply(moves_char, FUN = strsplit, split = " ")
ungerade <- sapply(moves_split, function(x) {seq(from = 1, to = length(x), by = 2)})
however, it will not let me choose the uneven numbers from the list.. I'm trying to get it to select a list of uneven numbers from each element of a list. I really hope I was able to formulate this understandably!
lapply(moves_split, FUN = "[[", sapply(moves_split, function(x) {seq(from = 1, to = length(x), by = 2)}))
Error in FUN(X[[i]], …) :
attempt to select more than one element in vectorIndex
Any help would be greatly appreciated
Hey Lola,
Thank you for the kind comment. Are you still seeking for help on this question? I just came back from vacation and therefore couldn’t reply earlier.
Regards,
Joachim
Face! You have no idea how this “map(my_list, 1) ” changed my life!”
haha ha! Amazing! I was exactly looking for this.
Haha, thank you so much for the great comment Wagner, glad it was helpful! 🙂
Regards,
Joachim
How to Extract mutiple Elements of Nested List Using purrr Package?
or extract any element and then obtain the difference from the original list?
Hi again Wagner,
You could do something like this to get separate lists:
However, I didn’t come up with a solution to keep this as a nested list. Please let me know if you find a solution for this, I’m also curious now.
Regards,
Joachim
Hi, Joachin!
I did find a solution! This is what I was looking for:
list <- list(1:3, 4:6, 7:9)
map2(list, map(list, 1), setdiff)
With the function map2, I could obtain the difference between the original list and the list in which I selected the first element.
Hi Wagner,
This is great, glad you found a solution and thanks for sharing! 🙂
Regards,
Joachim