[R-sig-Geo] subsetting Spatial*DataFrame

Agustin Lobo alobolistas at gmail.com
Fri Jul 3 19:46:10 CEST 2009


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



-------------- next part --------------
A non-text attachment was scrubbed...
Name: alobolistas.vcf
Type: text/x-vcard
Size: 251 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20090703/b68342ef/attachment.vcf>


More information about the R-sig-Geo mailing list