list 从列表中选择多个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12119019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select multiple elements from a list
提问by user1357015
I have a list in R some 10,000 elements long. Say I want to select only elements, 5, 7, and 9. I'm not sure how I would do that without a for loop.
我在 R 中有一个大约 10,000 个元素的列表。假设我只想选择元素 5、7 和 9。如果没有 for 循环,我不确定如何做到这一点。
I want to do something like mylist[[c(5,7,9]]
but that doesn't work. I've also tried the lapply
function but haven't been able to get that working either.
我想做类似的事情,mylist[[c(5,7,9]]
但这不起作用。我也尝试过该lapply
功能,但也无法使其正常工作。
回答by Glen_b
mylist[c(5,7,9)]
should do it.
mylist[c(5,7,9)]
应该这样做。
You want the sublists returned as sublists of the result list; you don't use [[]]
(or rather, the function is [[
) for that -- as Dason mentions in comments, [[
grabs the element.
您希望子列表作为结果列表的子列表返回;你不使用[[]]
(或者更确切地说,函数是[[
) - 正如 Dason 在评论中提到的那样,[[
抓取元素。