解决 dotsInPolys 错误的建议 (maptools) [英] Advice on troubleshooting dotsInPolys error (maptools)

查看:45
本文介绍了解决 dotsInPolys 错误的建议 (maptools)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 R 还很陌生,并且仍在学习一些解决我遇到的问题的方法.我遇到了一个我一直坚持的问题,想知道是否有人有建议.

I am pretty new to R and am still learning some of the ways to troubleshoot problems I encounter. I'm running into one that I'm stuck on and wondered if anyone has suggestions.

我正在尝试构建点密度图,但在使用 dotsInPolys 函数时遇到错误.行:

I am trying to build a dot density map, but I'm running into an error with the dotsInPolys function. The line:

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

文档表明 sccpolysplotvar 需要相同的长度,但我不确定如何仔细检查,或者更重要的是,纠正问题.有没有人对我如何检查出了什么问题有什么建议?提前致谢.

The documentation indicates that sccpolys and plotvar need to be the same length, but I'm unsure on how to double-check that, or, more importantly, correct the problem. Does anyone have recommendations on how I can check what's wrong? Thanks ahead of time.

这是我正在处理的整套代码:

Here's the entire set of code I'm working on:

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(sccpolys@data, sccpop.sub, sort=FALSE)
plotvar <- sccdata$C0X001 / 1000 # one dot per 1,000 people
head(sccpolys@data)
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")

推荐答案

@LincolnMullen 是对的.合并后,您有:

@LincolnMullen is right. After your merge you have:

> length(sccpolys)
[1] 787

> length(plotvar)
[1] 210

为了解决这个问题,你可以替换

To account for this you could replace

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

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

这篇关于解决 dotsInPolys 错误的建议 (maptools)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆