gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:09:30 26 4
gpt4 key购买 nike

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

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[[to_extract]]

`[[`(myList, to_extract)

library(purrr)
pluck(myList, to_extract)

关于R - 当名称在变量中时按名称提取列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46391701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com