[R-sig-Geo] Creating Regions With a Dummy Variable Identifier

Roger Bivand Roger.Bivand at nhh.no
Sat Jan 22 10:15:39 CET 2011


On Fri, 21 Jan 2011, Matthew Hasbrouck wrote:

>> Date: Fri, 21 Jan 2011 08:11:20 +0100
>> From: Roger.Bivand at nhh.no
>> To: mbh02 at live.com
>> CC: r-sig-geo at r-project.org
>> Subject: Re: [R-sig-Geo] Creating Regions With a Dummy Variable Identifier
>> On Thu, 20 Jan 2011, Matthew Hasbrouck wrote:
>>> Hello everyone,
>>> I am a graduate student currently working with census tract data. I have
>>> identified census tracts as either having a certain characteristic or
>>> not, using a dummy variable. This variable is coded as 1 if the tract
>>> has that characteristic and 0 if it doesn't. I would like to take all
>>> contiguous tracts coded as 1 and dissolve them into single records. What
>>> would be left are all of the original tracts that do not have this
>>> characteristic, coded as 0, and records for the larger dissolved areas
>>> that contained this characteristic. I originally thought the SKATER
>>> function might work, but I don't think it will work properly with a
>>> dummy variable.
>> Please see ?unionSpatialPolygons in maptools. You'll need to construct a
>> new set of ID values, with your tracts to be dissolved having the same ID,
>> and all the others having their own different IDs. Note the warning about
>> the license of gpclib in case you are affected.
>> Roger
>>> Thanks,
>>> Matt
> I think I understand a way to do this using unionSpatialPolygons if I
> have a fairly small number of tracts.  I could view a map of the dummy
> variable and manually give a unique ID to all tracts in a particular
> cluster.  I could continue doing that for all other clusters.  Then I
> could save the shapefile and union them in R.  The problem is
> that I am dealing with over 9,000 tracts in 91 cities.  Is there a
> systematic way to identify all of the contiguous 1's (on the dummy
> variable) and give them an ID?

So you have two questions, not one? You want to identify contiguous groups 
of 1s on the dummy, and group them into separate Polygons objects, and 
then merge the m contiguous groups so identified. I suggest subsetting the 
input object to just the 1s, then use poly2nb() to get the contiguities, 
next n.comp.nb() to identify the groups, unionSpatialPolygons() them, and 
spRbind() back the 0s. I would have liked to provide an example but have 
to rush ...

Hope this helps,


> Thanks,
> Matt
> 	[[alternative HTML version deleted]]
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> 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

More information about the R-sig-Geo mailing list