Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)

Let's say I have a list like this:

x = list(list(1,2), list(3,4), list(5,6))

I would like a list that contains only the first elements of the nested list. I can do this by returning another list like so

x1 = lapply(x, function(l) l[[1]])

Is there shortcut notation for this?

1 Answer

0 votes
by

To select only the first element of a nested list, you can do the following:

x = list(list(1,2), list(3,4), list(5,6))

lapply(x, `[[`, 1)

[[1]]

[1] 1

[[2]]

[1] 3

[[3]]

[1] 5

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...