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

Franziska Schulze franziska.schulze at wiwi.hu-berlin.de
Mon Nov 15 09:26:39 CET 2010


It works now. Am I sure that it puts my new variable as last column in my 
frame? Is it possible to sort the spatial polygons data frame with respect 
to one variable?


----- Original Message ----- 
From: "Barry Rowlingson" <b.rowlingson at lancaster.ac.uk>
To: "Franziska Schulze" <franziska.schulze at wiwi.hu-berlin.de>
Cc: <r-sig-geo at stat.math.ethz.ch>
Sent: Friday, November 12, 2010 4:28 PM
Subject: Re: [R-sig-Geo] problem with adding data to 
spatialpolygonsdataframe


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