从列表中选择多个元素 [英] Select multiple elements from a list

查看:181
本文介绍了从列表中选择多个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一个大约10,000个元素的列表.假设我只选择5、7和9个元素.我不确定如果没有for循环该怎么做.

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.

我想做类似mylist[[c(5,7,9]]的操作,但这不起作用.我也尝试过lapply函数,但也无法使其正常工作.

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)]应该这样做.

您希望将子列表作为结果列表的子列表返回;您不必为此使用[[]](或者功能是[[),就像Dason在评论中提到的那样,[[会抓取元素.

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.

这篇关于从列表中选择多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆