[R] merge and polylist

Roger Bivand Roger.Bivand at nhh.no
Sun Oct 8 22:10:16 CEST 2006


On Sat, 7 Oct 2006, Mihai Nica wrote:

> Greetings:
> 
> I would like to kindly ask for a little help. The rough code is:
> 

Maybe R-sig-geo would be a more relevant list. Comments inline.

> #________________________________________________________
> 
> dat=data.frame(read.delim(file="all.txt", header = TRUE, sep = "\t",
> quote="\"", dec=".",na.strings = "NA"))

We do not know if dat is in the same order as the shapefile. If dat$cod is 
malformed wrt. nc$att.data$AREA (very strange choice, in ESRI generated 
files often the polygon area), you are asking for trouble.

> 
> nc=read.shape("astae.shp", dbf.data=TRUE, verbose=TRUE)
> 
> mappolys=Map2poly(nc)
> 
> submap <- subset(mappolys, nc$att.data$NAME!="Honolulu, HI")
> 
> nc$att.data=subset(nc$att.data, nc$att.data$NAME!="Honolulu, HI")
> 

In situations like this, overwriting the input is not advisable, bercause
you destroy your ability to check that the output corresponds to your
intentions.
  
> nc$att.data[,1]=as.numeric(paste(nc$att.data$MSACMSA))
> 
> #attributes(nc$att.data)
> 
> nc$att.data=merge(nc$att.data, dat, by.x="AREA", by.y="cod", all.x=TRUE,
> sort=FALSE)

Ditto.

> 
> #attributes(nc$att.data)
> 
> tmp=file("tmp")
> 
> write.polylistShape(submap, nc$att.data, "tmp")
> 

Any good reason for not using the sp class framework? The objects you are 
using here are very low-level and messy.

library(maptools)
nc_1 <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
  ID="FIPS") # put shapefile in SpatialPolygonsDataFrame
nc_2 <- nc_1[coordinates(nc_1)[,1] < -80,] # subset with "[" method
row.names(as(nc_2, "data.frame"))
as.character(nc_2$FIPS)
tmpfl <- paste(tempfile(), "dbf", sep=".")
download.file("http://spatial.nhh.no/misc/nc_xtra.dbf", tmpfl, mode="wb")
nc.df <- read.dbf(tmpfl) # extra data keyed on CNTY_ID
nc_2$CNTY_ID
nc.df$CNTY_ID
nc_df2 <- merge(as(nc_2, "data.frame"), nc.df, by="CNTY_ID", sort=FALSE)
all.equal(nc_df2$CNTY_ID, nc_2$CNTY_ID)
row.names(nc_df2) <- nc_df2$FIPS # re-instate IDs
nc_3 <- SpatialPolygonsDataFrame(as(nc_2, "SpatialPolygons"), data=nc_df2)
# (if data row names do not match polygon IDs, will reorder, or fail if 
# any differ or absent
writePolyShape(nc_3, "nc_3")

This still isn't as tidy as it could be, but gives much more control than 
the original old-style classes.

Roger


> #_________________________________________________________
> 
> All works fine, but merge() changes the rownames and the link between the
> polygons and the corresponding rows is lost. I tried numerous other
> solutions (such as to "paste" back the old rownames), to no avail. After a
> few days, here I am. Please, if you have a moment, send a tip.
> 
>  Thanks,
> 
>  mihai
> 
> 
> 

-- 
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-help mailing list