Select multiple elements from a list

RListSubset

R Problem Overview


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.

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.

R Solutions


Solution 1 - R

mylist[c(5,7,9)] should do it.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1357015View Question on Stackoverflow
Solution 1 - RGlen_bView Answer on Stackoverflow