[R-sig-Geo] How to neighbours list functions match coords?

Edzer Pebesma edzer.pebesma at uni-muenster.de
Tue Nov 9 21:19:45 CET 2010


On 11/09/2010 09:05 PM, Lee Hachadoorian wrote:
> 
> 
> On 11/09/2010 02:12 PM, Roger Bivand wrote:
>> On Tue, 9 Nov 2010, Lee Hachadoorian wrote:
>>
>>> The example from the documentation for nbdists() uses two predefined
>>> objects (columbus and col.gal.nb), so it's not clear to me how
>>> coordinates are being matched (see below)
>> Good question. None of these functions check by matching, they assume
>> that the order is correct, and it is the user's responsibility to make
>> sure that it is. I'll try to revise the help pages to make this clearer.
>>
>> Do you think that the functions should check? If so, how, given that
>> by default matrices do not have row names?
> 
> This probably isn't too well thought out, but perhaps
> 
> (1) coordinates() could have a row.names parameter which, like poly2nb()
> would default to NULL. Then I would generate a matrix with row names using
> 
>> coords = coordinates(columbus, row.names = columbus$COLUMBUS_I)
> 
> (2) nbdists() and related functions would match based on row name,
> either automatically IF both the nb object and the coordinates matrix
> had row names, or with a parameter like match.ID = TRUE.
> 
> The background here is that I am a recent R convert coming from the SQL
> world, so I am used to matching things based on key columns rather that
> the "reorder the vectors and stick them next each other" approach that
> is common in the R world. So, for example to join attribute data to a
> SpatialPolygonsDataFrame, instead of the approach you use in your book
> of using match to generate an ordering vector then spCbind the attribute
> data frame, I will
> 
> row.names(spdf) = as.character(spdf$key_field)
> sp = as(spdf, "SpatialPolygons")
> row.names(df) = df$key_field
> spdfNew = SpatialPolygonsDataFrame(sp, df, match.ID = TRUE)

It would be rather trivial to make the following work:

spdfNew = SpatialPolygonsDataFrame(sp, df, match.ID = "key_field")

which would do the same as the last two of your commands.

> Partially my own limitations, but it does seem to use fewer lines of
> code when joining several different data frames of attribute data when
> I'm doing exploratory visualization.
> 
> --Lee
> 

-- 
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.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list