我对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 indicates是sccpolys
和plotvar
需要是相同的长度,但我对如何把握仔细检查,或更重要的是纠正问题。有没有人有如何检查有什么问题的建议?提前致谢。
这里的代码整套我工作的:
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")
您可以通过'length(sccpolys)'和'length(plotvar)'找到向量的长度。你可以使用'dput()'来获取重新创建R对象的代码。你介意分享你的数据的一部分吗?我的猜测是'length(sccpolys)== length(plotvar)'返回'FALSE',并且原因与'merge()'中的未对齐数据有关。 –
@LincolnMullen当然,数据应该[在这里](https://www.dropbox.com/sh/av051l7xugvlsdo/AABWThsv-dVy_Um7UzMSMnNLa?dl=0)。在'plotvar'和'sccpolys'上运行'length'确实会返回False ... –