代码之家  ›  专栏  ›  技术社区  ›  the_darkside

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

  •  1
  • the_darkside  · 技术社区  · 6 年前

    我有一个坐标对列表,是从空间数据框中提取出来的 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 ...
    

    我的目标是在每个字符串的每个坐标对上循环并打印坐标对。

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

    将按如下方式打印每个列表:

    [[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)
      }
    
    }
    

    如何更进一步得到每个坐标对?

    subscript out of bounds

    for (i in 1:length(res)){
    
      for (i in 1:length(res[i][[1]][[1]]))
      {
        print(res[1][[1]][[1]][i,])
      }
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Parfait    6 年前

    以apply家族中很少使用的成员为例, rapply

    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