[R-sig-Geo] subsetting Spatial*DataFrame

Edzer Pebesma edzer.pebesma at uni-muenster.de
Sat Jul 4 00:09:29 CEST 2009


Thanks, Augusting for pointing out a flaw in the [ methods for
Spatial*DataFrames. To be really honest, I didn't even know that you
could select rows by ID this way. After 16 years of using S!

I'll look into it when I get the chance; this may however take (quite) a
while.
--
Edzer

Agustin Lobo wrote:
> I apologize for raising the same topic from time to time,
> but each time I have to subset  Spatial*DataFrame
> objects (where * stands for polygons, lines or points) I
> get some problems.
>
> Last year Roger was kindly advising:
> "Just think of Spatial*DataFrame objects as data.frame objects and things
> will be much clearer"
>
> Nevertheless, when I have to subset my lines now, I find that, given
> > str(fl1,max.level=2)
> Formal class 'SpatialLinesDataFrame' [package "sp"] with 4 slots
>  ..@ data       :'data.frame':    4 obs. of  2 variables:
>  ..@ lines      :List of 4
>  ..@ bbox       : num [1:2, 1:2] 448617 4630059 450088 4631874
>  .. ..- attr(*, "dimnames")=List of 2
>  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>
> with
> > fl1 at data
>  CodLin Comment
> 0      1    <NA>
> 1      2    <NA>
> 2      3    <NA>
> 3      4    <NA>
>
> I can subset the data frame either as
> > fl1 at data[1,]
>  CodLin Comment
> 0      1    <NA>
>
> or as
>
> > fl1 at data["0",]
>  CodLin Comment
> 0      1    <NA>
>
> but for the  SLDF,  while  I  can use
> > plot(fl1[1,])
> I cannot use:
> > plot(fl1["0",])
> Error: trying to get slot "Lines" from an object of a basic class
> ("NULL") with no slots
>
> So what would be the way of subsetting by IDs?
>
> Also, Roger advised to get the IDs through either
> > sapply(slot(fl1, "lines"), function(x) slot(x, "ID"))
> [1] "0" "1" "2" "3"
> or
> > rownames(as.data.frame(fl1))
> [1] "0" "1" "2" "3"
>
> Considering that getting the IDs is an important task, would not
> be appropriate  having  an specific  function,  as  in the case of
> coordinates()?
> For example,  ID(fl1) would be a lot easier to remember.
>
> (I know I can do that function for my personal use, but then you end
> up not using standard R)
>
> Thanks!
>
> Agus
>
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>   

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de/
http://www.springer.com/978-0-387-78170-9 e.pebesma at wwu.de



More information about the R-sig-Geo mailing list