list 如何从R中的列表中进行子集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9624169/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:48:35  来源:igfitidea点击:

How to subset from a list in R

rlistsapply

提问by user1257894

I have a rather simple task but haven't find a good solution.

我有一个相当简单的任务,但还没有找到一个好的解决方案。

> mylist  
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10

[[2]]
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"

[[3]]
[1] 25 26 27 28 29 30 31 32

y <- c(3,5,9)    

I would like to extract from mylistthe sub-elements 3,5, and 9 of each component in the list. I have tried, sapply[mylist,"[[",y]but not luck!, and others like vapply, lapply, etc.. Thanks in advance for your help

我想从mylist 中提取列表中每个组件的子元素 3,5 和 9。我已经尝试过了,sapply[mylist,"[[",y]但运气不佳!还有其他人喜欢 vapply、lapply 等。在此先感谢您的帮助

Mauricio Ortiz

毛里西奥·奥尔蒂斯

回答by sgibb

You could use sapply(mylist, "[", y):

你可以使用sapply(mylist, "[", y)

mylist <- list(1:5, 6:10, 11:15)
sapply(mylist, "[", c(2,3))

回答by joran

Try using [instead of [[(and depending on what you're after you light actually want lapply).

尝试使用[而不是[[(并取决于您点亮后实际想要的东西lapply)。

From ?'[[':

来自?'[['

The most important distinction between [, [[ and $ is that the [ can select more than one element whereas the other two select a single element.

[、[[ 和 $ 之间最重要的区别是 [ 可以选择多个元素,而其他两个选择单个元素。

回答by USER_1

Using lapply:

使用lapply

# create mylist
list1<-1:10
list2<-letters[1:26]
list3<-25:32
mylist<-list(list1,list2,list3)

# select 3,5,9th element from each list
list.2 <- lapply(mylist, function(x) {x[c(3,5,9)]})

回答by DanO

purrrprovides another solution for solving these kinds of list manipulations within the tidyverse

purrr提供了另一种解决方案来解决tidyverse 中的这些类型的列表操作

library(purrr)
library(dplyr)

desired_values <- c(1,3)

mylist <- list(1:5, letters[1:6], 11:15) %>% 
purrr::map(`[`,desired_values) 

mylist

回答by toby544

I don't think sgibb's answer gives what you would want. I suggest making a new function:

我不认为 sgibb 的回答给出了你想要的。我建议创建一个新功能:

subsetList <- function(myList, elementNames) {
    lapply(elementNames, FUN=function(x) myList[[x]])
}

Then you can use it like this:

然后你可以像这样使用它:

x <- list(a=3, b="hello", c=4.5, d="world")
subsetList(x, c("d", "a"))
subsetList(x, c(4, 1))

These both give

这些都给

[[1]]
[1] "world"

[[2]]
[1] 3

which is what you would want, I think.

这就是你想要的,我想。

回答by Umaomamaomao

An easy way to subset repeated named elements of a list, similar to other answers here.

一种对列表的重复命名元素进行子集化的简单方法,类似于此处的其他答案。

(so I can find it next time I look this question up)

所以我下次查这个问题时可以找到它

E.g., subset the "b"elements from a repeating list where each element includes an "a"and "b"sub-element:

例如,"b"从重复列表中对元素进行子集化,其中每个元素都包含一个"a""b"子元素:

mylist <- list(
  list(
    "a" = runif(3),
    "b" = runif(1)
  ),
  list(
    "a" = runif(3),
    "b" = runif(1)
  )
)

mylist
#> [[1]]
#> [[1]]$a
#> [1] 0.7547490 0.6528348 0.2339767
#> 
#> [[1]]$b
#> [1] 0.8815888
#> 
#> 
#> [[2]]
#> [[2]]$a
#> [1] 0.51352909 0.09637425 0.99291650
#> 
#> [[2]]$b
#> [1] 0.8407162

blist <- lapply(
  X = mylist,
  FUN = function(x){x[["b"]]}
)

blist
#> [[1]]
#> [1] 0.8815888
#> 
#> [[2]]
#> [1] 0.8407162

Created on 2019-11-06 by the reprex package(v0.3.0)

reprex 包(v0.3.0)于 2019 年 11 月 6 日创建

回答by user1234357

There are better ways of doing this, but here's a quick solution.

有更好的方法可以做到这一点,但这里有一个快速的解决方案。

# your values
list1<-1:10
list2<-letters[1:26]
list3<-25:32

# put 'em together in a list
mylist<-list(list1,list2,list3)

# function
foo<-function(x){x[c(3,5,9)]}

# apply function to each of the element in the list
foo(mylist[[1]])
foo(mylist[[2]])
foo(mylist[[3]])

# check the output

> foo(mylist[[1]])
[1] 3 5 9
> foo(mylist[[2]])
[1] "c" "e" "i"
> foo(mylist[[3]])
[1] 27 29 NA