[R-sig-Geo] UnionSpatialPolygons in the German DEU_adm3.RData file

Roger Bivand Roger.Bivand at nhh.no
Mon Jan 31 10:45:28 CET 2011


On Mon, 31 Jan 2011, DPajak wrote:

>
> Thank very much Roger. By doing this, I am loosing the information from the
> original GADM-file, right?
>
> So what else I have to do, to have a file with all the information I had
> before, so I can use IDs to match my data, color the map, ...
>
> I used the following function spplot function before:
>
> --
> Map = spplot(gadm, "NAME_3", col.regions=col, main="Germany", colorkey =
> FALSE, lwd=.6, col="white")

By merging the Polygons objects, you have "wiped" their aggregated 
attributes - because unionSpatialPolygons() only merges the Polygons 
objects. So you could use a standard aggregation tool to aggregate the 
data.frame object in the data slot, and re-assemble later:

library(maptools)
nc1 <- readShapePoly(system.file("shapes/sids.shp",
   package="maptools")[1], proj4string=CRS("+proj=longlat +datum=NAD27"))
lps <- coordinates(nc1)
ID <- cut(lps[,1], quantile(lps[,1]), include.lowest=TRUE)
reg4 <- unionSpatialPolygons(nc1, ID)
row.names(reg4)
df <- as(nc1, "data.frame")
sapply(df, class)
dfa <- aggregate(df[,9:14], list(ID), sum)
sapply(df[,9:14], sum)
sapply(dfa[,2:7], sum)
row.names(dfa) <- as.character(dfa$Group.1)
nc2 <- SpatialPolygonsDataFrame(reg4, dfa)

But, as always, you need to make choices in aggregation to the variables 
in the data frame that can be aggregated, and about how to do it.

Hope this helps,

Roger


> --
>
> Kinds regards
>
> Danny
>
>

-- 
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-sig-Geo mailing list