[R-sig-Geo] Merging data frame for SpPolDF

Agustin Lobo alobolistas at gmail.com
Thu Mar 19 13:08:15 CET 2009


Hi!

I often have to add more information to the data slot of
a SpPolDF imported from a shp file. I do it in this way, don't like
it too much and would like feed-back on a better way-

#Import shp
MMAMBmuni <- readOGR("C:/Pruebas/DUNS/MMAMBmuni", layer="MMAMBmuni")
#Extract the DF
MMAMBmuniDFori <- MMAMBmuni at data

#Make a new dataframe by merging with another DF
MMAMBmuniDFnew <-
merge(MMAMBmuniDFori,MMAMBempleados,by.x="MUNICIPI",by.y="CODMUN",all.x=T,sort=F)

The problem here is that there are a couple of towns in the by.x field
for which we do not any in by.y
As we have set all.x=T, we get a line for which the values from the
second dataframe are NA. But, despite stating sort=F, those cases are
not in the same row as they are in the first data.frame but appended at
the end of the new dataframe. This is bad news for us, as breaks
the order required for including the new dataframe as the data slot
of a new SpPolDF. Therefore, I have to reorder the new dataframe, thanks
to another field, IDgrafic:

MMAMBmuniDFnew<- MMAMBmuniDFnew[order(MMAMBmuniDFnew$ID_GRAFIC),]

and then copy the original row.names, required because the row.names are
the ones
making the link to the polygons in the future SpPolDF:

row.names(MMAMBmuniDFnew) <- row.names(MMAMBmuniDFori)

#Now we put the new DF in lieu of the older one:
MMAMBmuni2 at data <- MMAMBmuniDFnew

#and finally save as shp
writeOGR(MMAMBmuni2,dsn="C:/Pruebas/DUNS/MMAMBmuni2",layer="MMAMBmuni2",
driver="ESRI Shapefile")

Any suggestions on a better procedure? The problem is that sometimes I
forget reordering and get a wrong shp. Until now, I have always realized
the error, but I'm terrified by the idea of not realizing the error
sometime and using true garbage after that point...

Thanks

Agus



More information about the R-sig-Geo mailing list