[R-sig-Geo] problem with adding data to spatialpolygonsdataframe

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Nov 12 16:28:00 CET 2010


On Fri, Nov 12, 2010 at 11:27 AM, Franziska Schulze
<franziska.schulze at wiwi.hu-berlin.de> wrote:
> Dear all,
>
> I have a SpatialPolygonsDataFrame and I want to add data to the regions, i.e. I want to assign to every region in my data a value of a certain variable. I tried using spCbind, but after using this, the dimensions of my SpatialPolygonsDataFrame changed. What went wrong? Do I have to create a matrix with the region's IDs and the values of my variable?

 You can for the most part treat it like a data frame. For example,
using the spdf created in help("SpatialPolygonsDataFrame-class")

> summary(ex_1.7)
Object of class SpatialPolygonsDataFrame
Coordinates:
  min  max
x 0.5 10.5
y 0.5 10.5
Is projected: NA
proj4string : [NA]
Data attributes:
       x              y              z
 Min.   : 1.0   Min.   : 1.0   Min.   :1.702
 1st Qu.: 3.0   1st Qu.: 3.0   1st Qu.:2.626
 Median : 5.5   Median : 5.5   Median :3.123
 Mean   : 5.5   Mean   : 5.5   Mean   :3.127
 3rd Qu.: 8.0   3rd Qu.: 8.0   3rd Qu.:3.614
 Max.   :10.0   Max.   :10.0   Max.   :4.600
> ex_1.7$FOO=99
> summary(ex_1.7)
Object of class SpatialPolygonsDataFrame
Coordinates:
  min  max
x 0.5 10.5
y 0.5 10.5
Is projected: NA
proj4string : [NA]
Data attributes:
       x              y              z              FOO
 Min.   : 1.0   Min.   : 1.0   Min.   :1.702   Min.   :99
 1st Qu.: 3.0   1st Qu.: 3.0   1st Qu.:2.626   1st Qu.:99
 Median : 5.5   Median : 5.5   Median :3.123   Median :99
 Mean   : 5.5   Mean   : 5.5   Mean   :3.127   Mean   :99
 3rd Qu.: 8.0   3rd Qu.: 8.0   3rd Qu.:3.614   3rd Qu.:99
 Max.   :10.0   Max.   :10.0   Max.   :4.600   Max.   :99
>

The dimension will change because you are adding a new column - if the
number of rows have changed then something is going wrong...

Post some code?

Barry



More information about the R-sig-Geo mailing list