简体   繁体   English

遍历列表中的列表

[英]Iterate over list in list

I am currently trying to create a list of matrices from a list of lists. 我目前正在尝试从列表列表中创建矩阵列表。 The ingredient of the list of list are polygons ie vertice coordinates of each o the five vertices: 列表列表的成分是多边形,即五个o中每个顶点的顶点坐标:

$HEW18
     [,1]    [,2]
[1,] 595165.9 5688133
[2,] 595065.9 5688133
[3,] 595065.9 5688233
[4,] 595165.9 5688233
[5,] 595165.9 5688133

$HEW19
     [,1]    [,2]
 [1,] 593512.7 5672780
 [2,] 593412.7 5672781
 [3,] 593414.0 5672881
 [4,] 593514.0 5672880
 [5,] 593512.7 5672780

There is a total of 150 polygons in this file 此文件中总共有150个多边形

Now in order to create a spatialpolygonsdataframe, I have to create matrices from the "inside" lists, or a list of matrices... 现在,为了创建空间多边形数据框,我必须从“内部”列表或矩阵列表中创建矩阵。

I managed to do that for one single polygon like this: 我设法对一个多边形进行了如下操作:

#turn list to matrix
matrixHEW39 = matrix(c(forest$HEW39[1:5,]), nc=2, byrow = FALSE)

What I would like to do now is, to save some time and NOT write this for all 150 polys individually but rather create a for-loop for it. 我现在想做的是节省一些时间,而不是单独为所有150个多边形编写此代码,而是为其创建一个for循环。

The following code represents my unrefined "idea" of how it should work: 以下代码代表了我如何完善其工作原理的“想法”:

forest_list <-list()

for (i in forest) {
   matrix[i] = matrix(c(forest$i[1:5], nc =2, byrow = FALSE)
   forest_list[i]<-matrix[i]
}

You see, the problem here, or at least one of them is the "forest$i[1:5]" 您看,这里的问题,或其中至少一个是“ forest $ i [1:5]”

How can I select ONE of the polygons and within each row 1 -5 , as in the single polygon code? 如何像单个多边形代码中那样选择一个多边形并在每行1 -5内? Also, I need to maintain the names of each polygon? 另外,我需要维护每个多边形的名称吗?

I think you are a little confused here (happens easily when learning R!). 我认为您在这里有些困惑(学习R!时很容易发生)。 This is based on seeing you using forest_list[i]<-matrix[i] . 这是基于看到您使用forest_list[i]<-matrix[i] Lists can be accessed using either single-brackets [i] , double-brackets [[i]] , or the dollar-name convention as you have used: forest$HEW39 . 可以使用单括号[i] ,双括号[[i]]或您所使用的美元名称约定来访问列表forest$HEW39 The single-bracket takes a 'slice', which returns the data, but it is still a list. 单括号包含一个“切片”,该切片返回数据,但它仍然是一个列表。 The double brackets return the object at position 'i' of the list, same as dollar-name returns the list item with that name. 双括号将对象返回到列表中位置'i' ,与dollar-name返回具有该名称的列表项相同。 NB you can also use [["HEW39"]] . 注意,您也可以使用[["HEW39"]]

I think this is from where the confusion stems. 我认为这是造成混乱的地方。 I don't think you have a list of lists, just a plain list, but trying to access this list using the single-bracket returns a list (of length 1) of your matrix. 我认为您没有列表列表,只有普通列表,但是尝试使用单括号访问此列表会返回矩阵的列表(长度为1)。 So it looks like a list of lists. 因此,它看起来像一个列表列表。 And naturally, you try to create a matrix from that. 当然,您尝试从中创建一个矩阵。 But the matrix is there, you just need to access it using double-brackets. 但是矩阵在那里,您只需要使用双括号访问即可。

To answer your question, I have made a list of 5 matrices similar to yours and named them: 为了回答您的问题,我列出了与您相似的5个矩阵,并命名为:

forest <- lapply(c(1,2,3,4,5), function(f){replicate(2, rnorm(5))})
names(forest) <- paste0("HEW", 1:5)

NB: 注意:

class(forest[1])
[1] "list"
class(forest[[1]])
[1] "matrix"
class(forest$HEW1)
[1] "matrix"

To index over those, we can use a for-loop, and capture in a new list 要为这些索引,我们可以使用for循环,并捕获到新列表中

forest_list <- as.list(names(forest))

for (i in 1:length(forest)) {
    matrixi <- forest[[i]]
    forest_list[[i]] <- matrixi
}

If you do have a list of lists, eg: 如果确实有列表列表,例如:

forest_lol <- list(1)
forest_lol[[1]] <- forest_list

you can iterate over the primary list you want (eg [[1]] in this case): 您可以遍历所需的主列表(例如,在这种情况下,例如[[1]] ):

forest_list <- as.list(names(forest))

for (i in 1:length(forest_lol[[1]])) {
    matrixi <- forest_lol[[1]][[i]]
    forest_list[[i]] <- matrixi
}

Retaining names in new lists just means either capturing them in a vector during the for-loop, or more simply naming based on the previous list: 在新列表中保留名称仅意味着在for循环期间将其捕获到向量中,或更简单地根据前一个列表进行命名:

name_vector <- c()
for (i in 1:length(forest)) {
    name_vector <- c(name_vector, names(forest)[i])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM