Skip to content
Prev 7915 / 29559 Next

spdep: making nb object from csv data

On Fri, 26 Mar 2010, Philip A. Viton wrote:

            
I guess it is easier not to randomise the ids, but I think that we can get 
the fish out of this fish soup.

First read the csv file (into a data frame). Most likely the column 
headers will be taken as col.names, with the first column the row names. 
Check that the r_id match the column names, and the values in the first 
column. Use match() to do this, and examine the length of the output and 
its contents. If it looks OK, use the output of match to re-order the 
matrix created by dropping the first column of the csv data.frame and 
coercing with as.matrix().

If:

o <- match(r_id, col_nms)
m <- as.matrix(csv_df[, -1])
m1 <- m[o, o]

then mat2listw(m1)

should be what you need. However, do check everything twice, including the 
order of the arguments to match() - I always have to as they feel 
counter-intuitive. If you have some centroid coordinates in the data 
frame, use them with the plot method for nb objects to double-check.

Hope this helps,

Roger