2015-06-22 19 views
2

我对R很新,我仍在学习一些解决我遇到的问题的方法。我遇到了一个被困住的人,并想知道是否有人有建议。关于排除dotsInPolys错误的建议(maptools)

我想构建一个点密度映射,但我遇到了与dotsInPolys函数的错误。行:

scc.rand <- dotsInPolys(sccpolys, as.integer(plotvar), f="random") 

这给我的错误:

> sccdots.rand <- dotsInPolys(sccpolys, as.integer(plotvar), f="random") 
Error in dotsInPolys(sccpolys, as.integer(plotvar), f = "random") : 
    different lengths 

documentation indicatessccpolysplotvar需要是相同的长度,但我对如何把握仔细检查,或更重要的是纠正问题。有没有人有如何检查有什么问题的建议?提前致谢。

这里的代码整套我工作的:

library(maptools) 

# Population data 
sccpop <- read.csv("nhgis0010_ds98_1970_tract.csv", stringsAsFactors = FALSE) 
sccpop.sub <- sccpop[sccpop$COUNTY=="Santa Clara",c(1,3,21,22,23)] 

# Shapefile for Census tracts 
scctract.shp <- readShapePoly("1970-ca-tracts.shp") 
sccpolys <- SpatialPolygonsDataFrame(scctract.shp, data=as(scctract.shp, "data.frame")) 

# Merge datasets 
sccdata <- merge([email protected], sccpop.sub, sort=FALSE) 
plotvar <- sccdata$C0X001/1000 # one dot per 1,000 people 
head([email protected]) 
head(sccpop.sub) 

# Generate random dots in polygons 
sccdots.rand <- dotsInPolys(sccpolys, as.integer(plotvar), f="random") 

# County boundaries 
baycounties.shp <- readShapePoly("ca-counties-1970.shp") 
baycounties <- SpatialPolygonsDataFrame(baycounties.shp, data=as(baycounties.shp, "data.frame")) 

par(mar=c(0,0,0,0)) 
plot(baycounties, lwd=0.1) 

# Add dots 
plot(sccdots.rand, add=TRUE, pch=19, cex=0.1, col="#00880030") 
+1

您可以通过'length(sccpolys)'和'length(plotvar)'找到向量的长度。你可以使用'dput()'来获取重新创建R对象的代码。你介意分享你的数据的一部分吗?我的猜测是'length(sccpolys)== length(plotvar)'返回'FALSE',并且原因与'merge()'中的未对齐数据有关。 –

+0

@LincolnMullen当然,数据应该[在这里](https://www.dropbox.com/sh/av051l7xugvlsdo/AABWThsv-dVy_Um7UzMSMnNLa?dl=0)。在'plotvar'和'sccpolys'上运行'length'确实会返回False ... –

回答

1

@LincolnMullen是正确的。你的合并之后,你有:

> length(sccpolys) 
[1] 787 

> length(plotvar) 
[1] 210 

考虑到这一点,你可以用

sccdots.rand <- dotsInPolys(sccpolys[sccpolys$GISJOIN %in% sccdata$GISJOIN,], as.integer(plotvar), f="random") 
1

问题更换

sccdots.rand <- dotsInPolys(sccpolys, as.integer(plotvar), f="random") 

是,你有更多的小块(即shapefile中的多边形)比你想要的阴谋。圣克拉拉有787大片,并且只有210大片。此外,SpatialPolygonsDataFrame中的@data插槽有一些不必要的操作。这是一个清理合并代码的解决方案。

library(maptools) 

shp <- readShapePoly("1970-ca-tracts.shp") 
sccpop <- read.csv("nhgis0010_ds98_1970_tract.csv", stringsAsFactors = FALSE) 
sccpop.sub <- sccpop[sccpop$COUNTY=="Santa Clara",c(1,3,21,22,23)] 

shp <- merge(shp, sccpop.sub) 

现在我们有数据一个SpatialPolygonsDataFrame,但也有对所有非圣克拉拉县缺失值。我们想要像上面那样改变人口。下面的第一行进行转换,在数据框中添加一列。只是将其保留在数据框内而不是外部变量是最简单的。第二行过滤掉所有没有人口相关的多边形,即非圣克拉拉县。

[email protected]$plotvar <- as.integer([email protected]$C0X001/1000) 
shp <- shp[!is.na([email protected]$plotvar), ] 

现在我们可以像以前一样进行操作。

sccdots.rand <- dotsInPolys(shp, [email protected]$plotvar, f="random") 

baycounties.shp <- readShapePoly("ca-counties-1970.shp") 

par(mar=c(0,0,0,0)) 
plot(baycounties.shp, lwd=0.1) 
plot(sccdots.rand, add=TRUE, pch=19, cex=0.1, col="#00880030") 

Resulting plot

FWIW,我已经使用rgdal::readOGR()装载形状文件更好的成绩,但在这里maptools::readShapePoly()工作正常。