[R-sig-Geo] merging data with SpatialPolygonsDataFrame

Dylan Beaudette dylan.beaudette at gmail.com
Thu Jan 24 20:18:33 CET 2008


On Thursday 24 January 2008, Roger Bivand wrote:
> On Wed, 23 Jan 2008, stefan lhachimi wrote:
> > Hello,
> > I am having a spatial object by reading a shp-file by the
> > readShapePoly command. I now want to add data into the
> > SpatialPolygonsDataFrame from an external source (its a dataframe
> > which has a common key SHN_N). I am using the merge command, which I
> > thought works fine, unitl I realized that "map.kreise" now became just
> > a data.frame.
>
> merge() is a "false friend", because a SpatialPolygonsDataFrame can be
> coerced to a data.frame, which is what merge() does without asking.
>
> It may be best to say corece first, merge() the two data.frames, and
> rebuild the SpatialPolygonsDataFrame with SpatialPolygonsDataFrame().
>
> map.kreise.df <- as(map.kreise, "data.frame")
> map.kreise.df1 <- merge(map.kreise.df, INC, sort=FALSE, by.x="SHN_N",
>    by.y="SHN_N", all.x=TRUE, all.y=TRUE)
> map.kreise1 <- SpatialPolygonsDataFrame(as(map.kreise, "SpatialPolygons"),
>    data=map.kreise.df1)
>
> which may fail if INC and map.kreise.df have different row.names. The
> argument match.ID= (default TRUE) to SpatialPolygonsDataFrame matches the
> polygon IDs to the data= row.names - they must agree exactly though the
> data frame rows will be re-ordered to match the polygons if only the
> orders differ. Your concern here is real, which is why the argument is
> there.
>
> If you don't need merge to manipulate the data frames, you can go straight
> for:
>
> all.equal(row.names(INC), row.names(as(map.kreise, "data.frame"))
> map.kreise1 <- spCbind(map.kreise, INC)
>
> if the row.names of INC match the polygon IDs, and thus the row.names of
> as(map.kreise, "data.frame"), see ?spCbind in maptools, and the example
> included there.
>
> Hope this helps,
>
> Roger
>
> > My greatest fear in working with spatial objects is that, if I don't
> > have them in one object, I end up plotting a value for the wrong
> > region. So is there a way to use merge or what is the most appropriate
> > way to combine data?
> >
> > map.kreise<-readShapePoly("vg250krs",IDvar="KRS_ID",verbos=TRUE) #
> > reads the shape file
> > map.kreise<-merge(map.kreise,INC,sort = FALSE,by.map.kreise="SHN_N",
> > by.INC="SHN_N",all.map.kreise=T,all.INC=T)
> >
> > Thanks a lot in advance,
> > Stefan
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo at stat.math.ethz.ch
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Hi Roger. Thanks for contributing some answers to this. 

I was recently working with a colleague on developing some sample exercises 
for new students. Since joining new attribute data to a GIS layer's table is 
a very common operation we included some samples on how to do this within R. 
You have hinted at some possible ways to do it above, but do you have a 'best 
practices' approach to doing this using 'sp' methods and objects?

For example:

# contains an attribute col named 'veg_code'
veg <- readOGR(something.shp)

# code meanings: indexed by 'veg_code'
codes <- read.dbf(table.dbf)

# what is the best way to join up the attributes in 'veg' with the rows 
in 'codes' ?


As of now we are using merge to replace the dataframe slot of the original 
file. We first re-order the results from merge to match the original row 
ordering:


# an example file:
veg <- readOGR(dsn='ArcGISLabData/BrownsPond/', layer='vegevector')

# some example codes
veg_codes <- data.frame(code=1:4, meaning=c('code 1','code 2','code 3','code 
4'))

# join the original data table with the veg codes table
combined <- merge(x=veg at data, y=veg_codes, by.x='CODE', by.y='code')


# overwrite the original data frame with the combined version
# note that the original order needs to be restored
# since the original data was sorted on 'ID', we can use that to restore
# the correct order in the 'combined' dataframe:
v at data <- combined[order(combined$ID),]


In summary, is there a safer or preferred way to do this?

thanks,

Dylan









-- 
Dylan Beaudette
Soil Resource Laboratory
http://casoilresource.lawr.ucdavis.edu/
University of California at Davis
530.754.7341




More information about the R-sig-Geo mailing list