Skip to content
Back to formatted view

Raw Message

Message-ID: <alpine.LRH.2.00.0910232125090.31851@reclus.nhh.no>
Date: 2009-10-23T19:28:52Z
From: Roger Bivand
Subject: basic polyfon shapefile subsetting
In-Reply-To: <4AE20167.3010109@u.washington.edu>

On Fri, 23 Oct 2009, Chris Fowler wrote:

> I swore I would figure this out without going to the list, but I just don't 
> understand the data structure well enough to make this work. Easy pickings 
> for many of you on this list.
>
> I have an ArcGIS shapefile "base.shp" that has 23034 polygons (U.S. census 
> tracts). I want to end up with a subset of this file that has no 'islands' 
> and a neighbor list to accompany it. What am I doing wrong?
>
> baseShp<-readShapePoly("Export_Output.shp")	#read in a shapefile
> Q1B<-poly2nb(baseShp, row.names = baseShp$FIPS)	#1st order Queen
> summary(Q1B)	#731 with 0 neighbors we need to remove these
> x<-card(Q1B)	#create a vector with the number of neighbors
> baseShp$numNeighbors<-x	#assign that vector as an additional data column
> length(baseShp$numNeighbors)	#check to make sure all 23034 have value
> summary(baseShp$numNeighbors)	#...and that those values are what I
> 				#expect
> submap <- baseShp[baseShp$attr.data$numNeighbors >=1] #pull out those
> 				# with at least 1 neighbor

Use

baseShp[baseShp$numNeighbors >=1, ]

There are two mistakes, one the missing comma, the other the use of a 
non-existing column, which will result in baseShp[NULL >=1] being 
evaluated.

Roger

> plot(submap)		#something plots....
> length(submap$numNeighbors)  #0, no data transferred
> subm<-as.data.frame(submap)  # convert to data frame for easier looking
> str(subm)  #we still have 23034 observations and now we have no data
>
>
> So, why doesn't my subseting code bring data with it and why doesn't it 
> actually do the subsetting?
>
> Thanks in advance,
>
> Chris
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no