Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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 - Fatal编程技术网

R通过变量引用列表中的元素

R通过变量引用列表中的元素,r,R,我有一个包含6个元素的列表: { "a": { "b": { "strA": { "strB": { "c": { "$": 8888 } } } } } } 如果我手动在strA和strB的值中键入print(未列出(a$b$strA$strB)),它将起作用 但是,我想做的是在一个循环中使用strA和strB的不同值来遍历列表 for (i in 1:nrow(h)) { x=st

我有一个包含6个元素的列表:

{
 "a": {
  "b": {
    "strA": {
      "strB": {
        "c": {
          "$": 8888
        }
      }
    }
  }
 }
}
如果我手动在strA和strB的值中键入print(未列出(a$b$strA$strB)),它将起作用

但是,我想做的是在一个循环中使用strA和strB的不同值来遍历列表

for (i in 1:nrow(h)) {

  x=strsplit(x=h[i, 1], "\\.")   # this bit works for me
  y <- unlist(a)   # this bit works for me and gives me y[1] and y[2]

  if (x==a$b$y[1]$y[2]){   # this bit does not work yet
    <etc.>
  }
}  
for(1中的i:nrow(h)){
x=strsplit(x=h[i,1],“\\”);这个位适合我

y它不起作用,因为
$
右侧的对象没有得到计算,它被视为一个字符串(换句话说,R在$b中寻找一个名为
“y[1]”
的元素

要通过动态变量访问,请使用
a[[“b”][[y[1]][[y[2]]]]


但是,与深度嵌套的列表相比,您可能可以使用更好的数据组织。

mylist$myelement的替代方法是
mylist[['myelement']]
。在后者中,您可以传递字符串
x为什么这些元素嵌套在列表中如此之深?这可能有助于
dput
您的对象,这样我们就可以确保查看的是同一个对象。