R:如何从列表的每个内部元素中删除第一个元素而不将其转换为矩阵?
R: How do I remove the first element from each inner element of a list without converting it to matrix?
我有一个这样的列表
[[1]]
[1] a1 b1 c1
[[2]]
[1] a2 b2 c2
[[3]]
[1] a3 b3 c3
我想从它的每个部分中删除特定元素:
[[1]]
[1] a1 c1
[[2]]
[1] a2 c2
[[3]]
[1] a3 c3
我尝试了 tail
但删除了 "outer" 个元素。也许一些索引会做?
假设模式只是您想要删除第二个元素,
lapply(List, function(x) x[-2])
使用purrr::map
做
会更短
# setup some example data
nestedList = list(list(4,5,6),list(1,2,3))
# remove first element from each sublist
map(nestedList, tail, -1)
我有一个这样的列表
[[1]]
[1] a1 b1 c1
[[2]]
[1] a2 b2 c2
[[3]]
[1] a3 b3 c3
我想从它的每个部分中删除特定元素:
[[1]]
[1] a1 c1
[[2]]
[1] a2 c2
[[3]]
[1] a3 c3
我尝试了 tail
但删除了 "outer" 个元素。也许一些索引会做?
假设模式只是您想要删除第二个元素,
lapply(List, function(x) x[-2])
使用purrr::map
做
# setup some example data
nestedList = list(list(4,5,6),list(1,2,3))
# remove first element from each sublist
map(nestedList, tail, -1)