从列表中选择多个元素。

时间:2022-06-24 21:27:05

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里的10000个元素。假设我只想选择元素,5 7 9。我不知道没有循环该怎么做。

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函数,但也没能成功。

1 个解决方案

#1


99  

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在注释中提到的,[[获取元素]。

#1


99  

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在注释中提到的,[[获取元素]。