从列表中选择多个元素

我有一个 R 格式的列表,大约有10,000个元素。假设我只想选择元素5、7和9。如果没有 for 循环,我不知道该怎么做。

我想做一些像 mylist[[c(5,7,9]]的东西,但是那不起作用。我也尝试了 lapply功能,但还是不能让它工作。

161808 次浏览

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.