[R-sig-Geo] rbind and spRbind

Roger Bivand Roger.Bivand at nhh.no
Sun Jul 27 21:31:38 CEST 2008


On Sun, 27 Jul 2008, Agustin Lobo wrote:

> Acceptable, yes, but perhaps we should choose
> one or the other and, for sure, document the difference
> between spRbind and rbind.
> Personally, I find a bit odd spRbind() in that you get 2
> variables that you were not seeing in data slot (I understand
> they are there as coordinates) of the original
> objects. This is not " rbind-like". But perhaps
> there are advantages that I'm not considering.
> Just wanted to point this out and make sure
> there were not other consequences at choosing
> between both functions.

rbind tries to be "like" rbind. Note that there is no default rbind 
generic method in base R - it is just a function. The original function is 
mean to join vectors and matrices by stacking them by row. It has no 
concept of anything other than the data. There are issues with stacking 
ordered objects by row, be they time series or whatever - so it isn't 
clear how this ought to be done. The rbind and spRbind methods for 
Spatial* objects try to help, but because user needs differ, no choices 
will meet all needs. As I said, the addition of the coordinates comes from 
the behaviour of the coercion method for SpatialPointsDataFrame to 
data.frame.

There are no single good solutions here, perhaps other than dropping any 
similarity to the base rbind, IMHO.

Roger

>
> Also, sorry for not having included this info:
>
>>  str(ika1v4geo)
> Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
>   ..@ data       :'data.frame':	693 obs. of  23 variables:
>   .. ..$ IKAPUNTO: int [1:693] 1 2 3 4 5 6 7 8 9 10 ...
>   .. ..$ IKAID   : num [1:693] 1 1 1 1 1 1 1 1 1 1 ...
>   .. ..$ CN      : chr [1:693] "Ivasichi" "Ivasichi" "Ivasichi" 
> "Ivasichi" ...
>  .. ..$ dia     : chr [1:693] "2008-01-30" "2008-01-30" "2008-01-30" 
> "2008-01-30" ...
>  .. ..$ hora_obs: chr [1:693] " 8:48:21" " 8:55:17" " 9:00:22" " 9:07:13" 
> ...
>  .. ..$ GPSID   : chr [1:693] "1-20080328-115" "1-20080328-116" 
> "1-20080328-117" "1-20080328-118" ...
>   .. ..$ GPS     : int [1:693] 1 1 1 1 1 1 1 1 1 1 ...
>   .. ..$ N_PUNTO : int [1:693] 115 116 117 118 119 120 122 123 124 125 ...
>   .. ..$ GUIA    : chr [1:693] "Endracio" "Endracio" "Endracio" 
> "Endracio" ...
>  .. ..$ MONITOR : chr [1:693] "Julio Vie" "Julio Vie" "Julio Vie" "Julio 
> Vie" ...
>   .. ..$ COD_SP_C: int [1:693] 29 9 10 29 8 8 28 8 28 29 ...
>   .. ..$ SP_C    : chr [1:693] "tatu" "jochi colorado" "jochi pintado" 
> "tatu" ...
>   .. ..$ COD_OBS : int [1:693] 5 5 5 5 6 6 6 7 6 8 ...
>   .. ..$ OBS     : chr [1:693] "huella" "huella" "huella" "huella" ...
>   .. ..$ N       : num [1:693] 1 1 1 1 1 1 1 1 1 1 ...
>   .. ..$ VULN    : int [1:693] 1 1 NA 1 3 3 NA 3 NA 1 ...
>   .. ..$ VULNT   : num [1:693] 1 1 NA 1 3 3 NA 3 NA 1 ...
>   .. ..$ CL      : num [1:693] 1 1 1 1 1 1 1 1 1 1 ...
>   .. ..$ A       : num [1:693] 8 8 8 8 8 8 8 8 8 8 ...
>   .. ..$ H       : num [1:693] 1.91 1.91 1.91 1.91 1.91 ...
>   .. ..$ D       : num [1:693] 0.719 0.719 0.719 0.719 0.719 ...
>   .. ..$ S       : num [1:693] 4 4 4 4 4 4 4 4 4 4 ...
>   .. ..$ V       : num [1:693] 34 34 34 34 34 34 34 34 34 34 ...
>   ..@ coords.nrs : int [1:2] 8 9
>   ..@ coords     : num [1:693, 1:2] 724547 724413 724367 724282 724225 ...
>   .. ..- attr(*, "dimnames")=List of 2
>   .. .. ..$ : NULL
>   .. .. ..$ : chr [1:2] "UTMX" "UTMY"
>   ..@ bbox       : num [1:2, 1:2]  716934 8303862  735638 8325272
>   .. ..- attr(*, "dimnames")=List of 2
>   .. .. ..$ : chr [1:2] "UTMX" "UTMY"
>   .. .. ..$ : chr [1:2] "min" "max"
>   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>   .. .. ..@ projargs: chr NA
>
> Thanks!
>
> Agus
>
> Roger Bivand wrote:
>>  On Sat, 26 Jul 2008, Danlin Yu wrote:
>> 
>> >  Agustin:
>> > 
>> >  Since you are using the sp method for binding, I suppose the coordinates 
>> >  (in UTM project according to the name) are added automatically (as the 
>> >  added two new names are UTMX and UTMY). Why not check these two values 
>> >  to see what's in them.
>>
>>  Yes, that's right. Your (cryptic) SPDF is a SpatialPointsDataFrame (could
>>  be Pixels or Polygons too ...), where the as(, "data.frame") coercion
>>  method adds back the columns "hidden" by the coordinates()<- assignment
>>  step earlier. spRbind uses the coercion method, rbind grabs the data slot
>>  with the @ operator. Both are acceptable.
>>
>>  Roger
>> 
>> > 
>> >  Hope this helps.
>> > 
>> >  Danlin
>> > 
>> > 
>> >  Agustin Lobo wrote:
>> > >   I'm a bit confused with the following:
>> > > 
>> > >   I have a SPDF object ika1v4
>> > > 
>> > > >   dim(ika1v4geo at data)
>> > >   [1] 693  23
>> > > >   dim(ika1v4geo)
>> > >   [1] 693  23
>> > > 
>> > >   Then I do an rbind:
>> > > 
>> > > >   a <- rbind(ika1v4geo,ika1v4geo)
>> > > >   dim(a)
>> > >   [1] 1386   23
>> > > >   dim(a at data)
>> > >   [1] 1386   23
>> > > 
>> > >   But spRbind introduces 2 variables:
>> > > >   a2 <- spRbind(ika1v4geo,ika1v4geo)
>> > > >   dim(a2)
>> > >   [1] 1386   25
>> > > >   dim(a2 at data)
>> > >   [1] 1386   25
>> > > 
>> > > >   names(a at data)
>> > >    [1] "IKAPUNTO" "IKAID"    "CN"       "dia"      "hora_obs" "GPSID"
>> > >    [7] "GPS"      "N_PUNTO"  "GUIA"     "MONITOR"  "COD_SP_C" "SP_C"
>> > >   [13] "COD_OBS"  "OBS"      "N"        "VULN"     "VULNT"    "CL"
>> > >   [19] "A"        "H"        "D"        "S"        "V"
>> > > >   names(a2 at data)
>> > >    [1] "IKAPUNTO" "IKAID"    "CN"       "dia"      "hora_obs" "GPSID"
>> > >    [7] "GPS"      "UTMX"     "UTMY"     "N_PUNTO"  "GUIA"     "MONITOR"
>> > >   [13] "COD_SP_C" "SP_C"     "COD_OBS"  "OBS"      "N"        "VULN"
>> > >   [19] "VULNT"    "CL"       "A"        "H"        "D"        "S"
>> > >   [25] "V"
>> > > > 
>> > >   Why? Which are the differences between rbind and spRbind?
>> > > 
>> > >   Thanks!
>> > > 
>> > >   Agus
>> > 
>> >
>> 
>
>

-- 
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