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

Lee Hachadoorian Lee.Hachadoorian+L at gmail.com
Tue Nov 9 21:05:17 CET 2010



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)

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

-- 
Lee Hachadoorian
PhD Student, Geography
Program in Earth & Environmental Sciences
CUNY Graduate Center



More information about the R-sig-Geo mailing list