Skip to content
Prev 5297 / 29559 Next

Merging data frame for SpPolDF

Hi

It might be we are talking about different things. What I understand is that 
you have an original shp-file with unique IDs associated with each Polygon. 
In the shape-file these are named "letras". In addition to this you have a 
data.frame with variables "letters" which matches the ones in "letras". 

However in the letters column there are fewer IDs that in letras. This is what 
is generated by:
#First we extract the CNTY_ID (let this be letras) to get some similar IDs for 
the dummy data.frame. At this stage it should contain one column named ID 
with the IDs, and one named Ndata with some random values
extra <- data.frame(ID=slot(nc, 'data')$CNTY_ID, 
Ndata=runif(length(slot(nc, 'data')$CNTY_ID)))

first:
'data.frame':   100 obs. of  14 variables:
.
.
 $ CNTY_ID  : num  1825 1827 1828 1831 1832 ...                                  
.
. 
$ NWBIR79  : num  19 12 260 145 1197 ...
.


#The next step is to remove some of these IDs (1-3, and 68-100). At this stage 
we have removed quite a number of IDs
extra <- extra[4:67, 1:2]

#In addition we want to sort them in a different way, to make things more 
realistic
extra <- extra[order(extra$ID, decreasing=TRUE),]

#And finally we change the value of one of the IDs to have a value that is not 
present in the original CNTY_ID. Also this to make it more realistic
extra[1,1] <- 342
'data.frame':   64 obs. of  2 variables:
 $ ID   : num  342 2039 2034 2032 2030 ...
 $ Ndata: num  0.8272 0.0255 0.5633 0.1834 0.8208 ...
num [1:100] 1825 1827 1828 1831 1832 ...

So we see they look different, just like a1 and a2 in the example provided. So 
far we have only been worried about making the dummy data. 

Skip the merge() function and move directly to the match() function. This is 
what you want to do (only the next steps):

extra <- extra[match(slot(nc, 'data')$CNTY_ID, extra$ID), 1:2]
'data.frame':   100 obs. of  2 variables:
 $ ID   : num  NA NA NA 1831 1832 ...
 $ Ndata: num  NA NA NA 0.252 0.842 ...

We now have the data frame you wanted to add with the same number of rows, and 
ordered the same way as the data in the SpatialPolygonsDataFrame.

We add it to the data slot in the SpatialPolygonsDataFrame.
slot(nc, 'data')$Ndata <- extra$Ndata
'data.frame':   100 obs. of  15 variables:      
.
.
 $ CNTY_ID  : num  1825 1827 1828 1831 1832 ...                                  
 .
.
 $ NWBIR79  : num  19 12 260 145 1197 ...
 $ Ndata    : num  NA NA NA 0.252 0.842 ...
.

The point is that you sort the data by the IDs in the original shape file, and 
hence you can simply add the data back to the data slot and they are than 
located in the right place.

Finally export the data, and the new shapefile has one more variable. 
writeOGR(nc,dsn="/home/lunde/MMAMBmuni2",layer="MMAMBmuni2",
driver="ESRI Shapefile")


Am I still wrong? In that, could someone else assist me?

Best wishes 
Torleif
On Thursday 19 March 2009 08:21:25 pm Agustin Lobo wrote: