ephemeris
ephemeris

Reputation: 785

R: How do I remove the first element from each inner element of a list without converting it to matrix?

I have a list like that

[[1]]
[1] a1 b1 c1
[[2]]
[1] a2 b2 c2
[[3]]
[1] a3 b3 c3

I want specific element removed from each part of it:

[[1]]
[1] a1 c1
[[2]]
[1] a2 c2
[[3]]
[1] a3 c3

I tried tail but removes "outer" elements. Maybe some indexing would do?

Upvotes: 7

Views: 5708

Answers (2)

Holger Brandl
Holger Brandl

Reputation: 11192

Using purrr::map it would be even shorter by doing

# setup some example data
nestedList = list(list(4,5,6),list(1,2,3))

# remove first element from each sublist
map(nestedList, tail, -1)

Upvotes: 9

Señor O
Señor O

Reputation: 17412

Assuming the pattern is just that you want the second element removed,

lapply(List, function(x) x[-2])

Upvotes: 9

Related Questions