R-当名称在变量中时,按名称提取列表元素
当名称存储在变量中时,我尝试按名称提取列表元素。i、 e: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
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很好。这取决于要提取的元素数量<代码>[[
适用于单个元素。[
适用于多个元素,但仍将它们作为列表。