Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-当名称在变量中时,按名称提取列表元素_R_List - Fatal编程技术网

R-当名称在变量中时,按名称提取列表元素

R-当名称在变量中时,按名称提取列表元素,r,list,R,List,当名称存储在变量中时,我尝试按名称提取列表元素。i、 e: myList <- list(a = 1, b = 2, c = 3) ## list definition ## I can extract the second element like this: myList$b ## but say I have a variable: to_extract <- "b" ##can I do something like this? myList$to_extract myLi

当名称存储在变量中时,我尝试按名称提取列表元素。i、 e:

myList <- list(a = 1, b = 2, c = 3) ## list definition
## I can extract the second element like this:
myList$b
## but say I have a variable:
to_extract <- "b"
##can I do something like this?
myList$to_extract

myList以下各项都应该有效

myList[[to_extract]]

`[[`(myList, to_extract)

library(purrr)
pluck(myList, to_extract)

myList[to_extract]
将为您提供完整的元素。我有一个名称向量(字符),例如
v1=c('a','c')
MyList[[v1]]
不起作用,但是
MyList[v1]
起作用。@xm1很好。这取决于要提取的元素数量<代码>[[
适用于单个元素。
[
适用于多个元素,但仍将它们作为列表。