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_Loops_Matrix - Fatal编程技术网

在R中访问列表中的列表元素

在R中访问列表中的列表元素,r,list,loops,matrix,R,List,Loops,Matrix,我有一个坐标对列表,这些坐标对是从SpatialLinesData framesldf中提取的-列表中的每个列表代表一个单独的字符串: res <- lapply(slot(sldf, "lines"), function(x) lapply(slot(x, "Lines"), function(y) slot(y, "coords"))) > str(res) List

我有一个坐标对列表,这些坐标对是从SpatialLinesData frame
sldf
中提取的-列表中的每个列表代表一个单独的字符串:

res <- lapply(slot(sldf, "lines"), function(x) lapply(slot(x, "Lines"), 
                                                     function(y) slot(y, "coords"))) 


> str(res)
List of 1683
 $ :List of 1
  ..$ : num [1:130, 1:2] -122 -122 -122 -122 -122 ...
 $ :List of 1
  ..$ : num [1:120, 1:2] -122 -122 -122 -122 -122 ...
 $ :List of 1
  ..$ : num [1:162, 1:2] -122 -122 -122 -122 -122 ...
 $ :List of 1
  ..$ : num [1:34, 1:2] -122 -122 -122 -122 -122 ...
将打印每个列表,如下所示:

[[1]]
[[1]][[1]]
            [,1]     [,2]
  [1,] -122.4449 37.76559
  [2,] -122.4449 37.76559
  [3,] -122.4449 37.76559
  [4,] -122.4449 37.76559 ...
在列表中。更进一步,我能够打印每个列表的单独行号

for (i in 1:length(res)){

  for (i in 1:length(res[i][[1]][[1]]))
  {
    print(i)
  }

}
如何进一步获得每个坐标对

下面生成一个错误
下标超出范围

for (i in 1:length(res)){

  for (i in 1:length(res[i][[1]][[1]]))
  {
    print(res[1][[1]][[1]][i,])
  }

}

考虑apply家族中很少使用的成员,
rappy
(递归apply成员):

证明:

set.seed(9222018)
# NESTED LIST OF FIVE LISTS EACH WITH ONE 5 X 2 MATRIX
res <- lapply(1:5, function(x) list(replicate(2, runif(5))))

str(res)
# List of 5
#  $ :List of 1
#   ..$ : num [1:5, 1:2] 0.233 0.959 0.242 0.131 0.924 ...
#  $ :List of 1
#   ..$ : num [1:5, 1:2] 0.0347 0.0409 0.9717 0.1854 0.6874 ...
#  $ :List of 1
#   ..$ : num [1:5, 1:2] 0.579 0.994 0.339 0.554 0.188 ...
#  $ :List of 1
#   ..$ : num [1:5, 1:2] 0.306 0.828 0.29 0.416 0.57 ...
#  $ :List of 1
#   ..$ : num [1:5, 1:2] 0.722 0.117 0.292 0.32 0.131 ...

嗨,黑暗之边。我真的不明白你想要的输出是什么样子的,我也不确定你的列表是什么样子的。我认为如果你能提供一些数据示例和几行所需的输出,可能会有所帮助。对于嵌套For循环,命名计数变量可能会有所帮助,而不是同时命名i和j。因此,您可以在内部循环中使用res[[i][[j]]。
rapply(res, print)
set.seed(9222018)
# NESTED LIST OF FIVE LISTS EACH WITH ONE 5 X 2 MATRIX
res <- lapply(1:5, function(x) list(replicate(2, runif(5))))

str(res)
# List of 5
#  $ :List of 1
#   ..$ : num [1:5, 1:2] 0.233 0.959 0.242 0.131 0.924 ...
#  $ :List of 1
#   ..$ : num [1:5, 1:2] 0.0347 0.0409 0.9717 0.1854 0.6874 ...
#  $ :List of 1
#   ..$ : num [1:5, 1:2] 0.579 0.994 0.339 0.554 0.188 ...
#  $ :List of 1
#   ..$ : num [1:5, 1:2] 0.306 0.828 0.29 0.416 0.57 ...
#  $ :List of 1
#   ..$ : num [1:5, 1:2] 0.722 0.117 0.292 0.32 0.131 ...
out <- rapply(res, print, how="list")

#           [,1]      [,2]
# [1,] 0.2334018 0.4563486
# [2,] 0.9593926 0.8900761
# [3,] 0.2415238 0.1898711
# [4,] 0.1312646 0.2723704
# [5,] 0.9238483 0.5405712
#            [,1]      [,2]
# [1,] 0.03469751 0.6921262
# [2,] 0.04085011 0.9977958
# [3,] 0.97173617 0.7002101
# [4,] 0.18537097 0.7687420
# [5,] 0.68738469 0.8482499
#           [,1]       [,2]
# [1,] 0.5789794 0.53362949
# [2,] 0.9938713 0.06445358
# [3,] 0.3390548 0.56161016
# [4,] 0.5536486 0.69291413
# [5,] 0.1878046 0.34357447
#           [,1]      [,2]
# [1,] 0.3062696 0.8913562
# [2,] 0.8281726 0.7861409
# [3,] 0.2902253 0.3713141
# [4,] 0.4156087 0.8301594
# [5,] 0.5695427 0.5160663
#           [,1]      [,2]
# [1,] 0.7217106 0.3459698
# [2,] 0.1174953 0.4014062
# [3,] 0.2917907 0.6519540
# [4,] 0.3204130 0.6228116
# [5,] 0.1309318 0.9475084
identical(res, out)
# TRUE