问题
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?
回答1:
Assuming the pattern is just that you want the second element removed,
lapply(List, function(x) x[-2])
回答2:
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)
来源:https://stackoverflow.com/questions/34858384/r-how-do-i-remove-the-first-element-from-each-inner-element-of-a-list-without-c