Skip to content
Prev 15884 / 29559 Next

exporting neighbor list to a text file...

On Thu, 16 Aug 2012, Alok K Bohara, PhD wrote:

            
Do you have a reason for using this format rather than the standard GAL 
format, which is:

n
1 #neigh1
neigh11 neigh12 ...
2 #neigh2
neigh21 neigh22 ...

etc, that is two lines per observation, the first giving its ID and the 
count of neighbours, and which can be read by many applications?

The following will do what you want, but I don't think you should:

library(spdep)
data(columbus)
tf <- tempfile()
con <- file(tf, open="wt")
n <- length(col.gal.nb)
writeLines(paste(n), con=con)
for (i in seq(along=col.gal.nb))
  writeLines(paste(i, paste(col.gal.nb[[i]], collapse=" ")), con=con)
close(con)
file.show(tf)

read.gal() and write.nb.gal() handle settings in which the observations 
have IDs which are not necessarily the integers 1:n, and do sanity 
checking.

Hope this helps,

Roger