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

Torleif Markussen Lunde torleif.lunde at cih.uib.no
Thu Mar 19 15:29:18 CET 2009


Hi

Maybe this can help? Please correct me if this is not what you wanted.

require(maptools)

nc <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], 
		    proj4string=CRS("+proj=longlat +datum=NAD27"))

#Create dummy data. Do some changes to make it look different (subset and 
order)
extra <- data.frame(ID=slot(nc, 'data')$CNTY_ID, 
Ndata=runif(length(slot(nc, 'data')$CNTY_ID)))
extra <- extra[4:67, 1:2]
extra <- extra[order(extra$ID, decreasing=TRUE),]
extra[1,1] <- 342

#add the dummy data(.frame) (this part is what you want to do)
extra <- extra[match(slot(nc, 'data')$CNTY_ID, extra$ID), 1:2]

slot(nc, 'data')$Ndata <- extra$Ndata
#or for the data frame
slot(nc, 'data') <- cbind(slot(nc, 'data'), extra[-1])

Best wishes
Torleif



On Thursday 19 March 2009 01:08:15 pm Agustin Lobo wrote:
> 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,s
>ort=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
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list