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

烂漫一生 提交于 2019-12-21 20:50:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!