使用屏蔽向量选择大SpatialPolygonsDataFrame的子集

时间:2016-06-07 08:15:32

标签: r s4 sp

任何人都可以帮助我理解为什么这不起作用我认为它应该如何?

我有一个1,000+元素SpatialPolygonsDataFrame对象(即来自sp),我想将其从整体上剪切为20个随机项目的向量。

n <- dim(geo1) # where geo1 is a big SpatialPolygonsDataFrame object
n <-n [1]
set.seed(1) # set random number generator seed for repeatability.
new_n=20    # Choose 20 random polygons
mask <- sample(0:n,new_n,replace=F) # trim size down from n to new_n polygons.
geo1small <- SpatialPolygonsDataFrame(Sr = geo1@polygons[mask], data = geo1@data[mask,], match.ID = TRUE)

结果:

Error in SpatialPolygonsDataFrame(Sr = geo1@polygons[mask], data = geo1@data[mask,  : 
trying to get slot "polygons" from an object of a basic class ("list") with no slots

1 个答案:

答案 0 :(得分:2)

您可以按索引直接对空间对象进行子集化:

geo1small <- geo1[mask, ]