[R-sig-Geo] Join Spatial Polygons to data.frame in R?

Roger Bivand Roger.Bivand at nhh.no
Tue Jun 9 09:01:03 CEST 2009


On Mon, 8 Jun 2009, Yuri Zhukov wrote:

> All,
>
> Is there a function in R similar to "Join attributes from a table" in ArcGIS
> 9x? I am trying to join a SpatialPolygonsDataFrame (created from an ESRI
> shapefile) to a data.frame which has the same unique ID, but contains some
> missing values. As a result of the missing data issues, neighbor objects
> created from the SpatialPolygonsDataFrame  -- particularly contiguity
> neighbors that require polygons to remain intact -- are of different length
> than the number of rows in the data.frame and spatially lagged terms cannot
> be created.

Please see the spCbind methods in maptools, ?spCbind. Note that you will 
need to insert the missing rows into the data frame first, and ensure that 
the row names of the data frame in the data slot of the spatial object are 
identical with those of the data frame being added. If this is awkward, 
consider extracting a skeleton data frame from the spatial object first, 
using merge() next, then using spCbind() finally to add it back. The chief 
reason for this being harder than just using merge() is that merge() can 
reorder rows, something that would not be helpful here.

You can look at the code of the spCbind method with:

getMethod("spCbind", c("SpatialPolygonsDataFrame", "data.frame"))

if you want to see what it is doing.

Hope this helps,

Roger

>
> The merge() command from the base package allows one to join information
> from the two objects, but treats both inputs as standard data.frame objects
> and produces a data.frame as the joined output. What I need is a function
> that yields a SpatialPolygonsDataFrame object as the output.
>
> Any help would be greatly appreciated.
>
> Thank you in advance,
> Yuri
>
> ---
> Yuri M. Zhukov
> Ph.D. Student
> Department of Government
> Harvard University
> zhukov at fas.harvard.edu
>
> _______________________________________________
> 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



More information about the R-sig-Geo mailing list