# 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:

In addition, you might have a look at the other articles on my website:

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.

Subscribe to the Statistics Globe Newsletter

• 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:

```map(my_list, 1)
map(my_list, 2)```

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.