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,我有一个特别的要求,我事先不知道需要多少级别的分支。我需要的数据结构是一个列表,因为我也不知道有多少元素,我将存储在每个分支 现在我正在将分支硬编码为1、2、3、4或5。我最初认为我永远不会要求超过3个层次。虽然现在不是这样。我已经降到5级了;每次只是更新下面的代码 # list of lists of lists of ... # i.e. declaring the data structure that holds the indices for each portfolio!

我有一个特别的要求,我事先不知道需要多少级别的分支。我需要的数据结构是一个列表,因为我也不知道有多少元素,我将存储在每个分支

现在我正在将分支硬编码为1、2、3、4或5。我最初认为我永远不会要求超过3个层次。虽然现在不是这样。我已经降到5级了;每次只是更新下面的代码

  # list of lists of lists of ...
  # i.e. declaring the data structure that holds the indices for each portfolio!
  l = vector("list", num_ports[1]);
  if(length(num_ports) > 1) {   for(i in 1:num_ports[1]) {

    l[[i]] = vector("list", num_ports[2]);

    if(length(num_ports) > 2) {   for(j in 1:num_ports[2]) {

      l[[i]][[j]] = vector("list", num_ports[3]);

      if(length(num_ports) > 3) {   for(k in 1:num_ports[3]) {

        l[[i]][[j]][[k]] = vector("list", num_ports[4]);

        if(length(num_ports) > 4) {   for(k in 1:num_ports[4]) {

          l[[i]][[j]][[k]][[p]] = vector("list", num_ports[5]);

          # more than 4 levels not allowed as of now!
          # index the list by: l[[i]][[j]][[k]][[p]][[q]] where q in 1:num_ports[5]

        }
        }
      }
      }
    }
    }
  }
  }
我知道要在其上创建层次结构的数组
num\u port


注意:我稍后会更新并访问此列表。那里的代码更混乱。我可能永远不需要超过5个级别,但目前的方式远远不够优雅。

这可以简化很多
l[[i]][[j]][[k][[p]]
l[[c(i,j,k,p)]]
是一回事。你可以用任何方法构造索引向量,你不需要事先知道它需要多长时间。

这可以简化很多
l[[i]][[j]][[k][[p]]
l[[c(i,j,k,p)]]
是一回事。你可以用任何方法构造索引向量,你不需要事先知道它需要多长时间。

这就是你如何构建“空”嵌套列表的方法:

Reduce(function(x, y) replicate(y, x, F), rev(num_ports), NULL)

对于
num\u-ports这是如何构建“空”嵌套列表的:

Reduce(function(x, y) replicate(y, x, F), rev(num_ports), NULL)

对于
num\u端口,这是一个很好的解决方案!这正是我需要的。谢谢:)关于如何使用与您相同的名称访问/更新它,即不依赖于
num\u端口长度的名称,您有什么想法吗?
?很好的解决方案!这正是我需要的。谢谢:)关于如何使用与您相同的名称访问/更新它,即不依赖于
num\u端口长度的名称,您有什么想法吗?