将列表中的所有SpatialPolygonsDataFrame对象聚合到一个SpatialPolygonsDataFrame中

将列表中的所有SpatialPolygonsDataFrame对象聚合到一个SpatialPolygonsDataFrame中,r,rbind,sp,spdf,R,Rbind,Sp,Spdf,不需要编辑拓扑,只需将所有多边形聚合为一个sp类型为SpatialPolygonsDataFrame(spdf)的对象。每个spdf只有一个多边形 数据()(文件大小1.1KB)(dput()在本例中不适用): 很明显,这会返回一个列表,因此不是我想要的 所以我写了一个函数: rbindSPDF <- function(lst) { # Create empty spdf objects pol <- SpatialPolygonsDataFrame(SpatialPol

不需要编辑拓扑,只需将所有多边形聚合为一个
sp
类型为
SpatialPolygonsDataFrame
(spdf)的对象。每个spdf只有一个多边形

数据()(文件大小1.1KB)(
dput()
在本例中不适用):

很明显,这会返回一个列表,因此不是我想要的

所以我写了一个函数:

rbindSPDF <- function(lst) {
# Create empty spdf objects  
pol <-
    SpatialPolygonsDataFrame(SpatialPolygons(list()), data = data.frame())
  pols <-
    SpatialPolygonsDataFrame(SpatialPolygons(list()), data = data.frame())
# loop for rbind
  for (i in 1:length(lst)) {
    pol[i] <- lst[i][[1]]
    if (length(pols) == 0) {
      pols <- pol[i]
    } else {
      pols <- rbind(pols, pol[i], makeUniqueIDs = TRUE)
    }
  }
  return(pols)
}
我得到:

Error in as.vector(data) : 
 no method for coercing this S4 class to a vector
不知道我做错了什么

另外,我猜我甚至不需要使用自己的函数


注意:除了许多其他软件包之外,我正在使用
sp
rgdal
来处理空间数据,由于附加/分离时间和掩蔽,我宁愿避免使用另一个软件包。

以获得

one_spdf <- rbind(list_of_spdf[1][[1]], 
                  list_of_spdf[2][[1]], 
                  list_of_spdf[3][[1]], 
                  ...
                  makeUniqueIDs = TRUE)

在我的机器上,结果似乎相当。

谢谢@Z.Lin。我想如果我能将,
makeUniqueIDs=TRUE
添加到
do.call()
中,它可能会起作用。我怎样才能做到呢?嗯,
do.call(“rbind”,args=c(list.df,makeUniqueIDs=TRUE))
对你有用吗?明白了,谢谢:
one\u spdf
rbindSPDF <- function(lst) {
# Create empty spdf objects  
pol <-
    SpatialPolygonsDataFrame(SpatialPolygons(list()), data = data.frame())
  pols <-
    SpatialPolygonsDataFrame(SpatialPolygons(list()), data = data.frame())
# loop for rbind
  for (i in 1:length(lst)) {
    pol[i] <- lst[i][[1]]
    if (length(pols) == 0) {
      pols <- pol[i]
    } else {
      pols <- rbind(pols, pol[i], makeUniqueIDs = TRUE)
    }
  }
  return(pols)
}
single_spdf <- rbindSPDF(list_of_spdf)
Error in as.vector(data) : 
 no method for coercing this S4 class to a vector
one_spdf <- rbind(list_of_spdf[1][[1]], 
                  list_of_spdf[2][[1]], 
                  list_of_spdf[3][[1]], 
                  ...
                  makeUniqueIDs = TRUE)
# generate list containing list_of_spdf[i][[1]]
list.df <- lapply(seq_along(list_of_spdf),
                  function(i){list_of_spdf[i][[1]]})

# apply rbind to the list
one_spdf2 <- do.call("rbind",
                     c(args = list.df, makeUniqueIDs = TRUE))

> all.equal(one_spdf, one_spdf2)
[1] TRUE