[R-sig-Geo] Adding a Few Neighbour Relationships to a nb List

Vijay Lulla v|j@y|u||@ @end|ng |rom gm@||@com
Thu Aug 8 01:11:36 CEST 2019


In addition to Barry's recommendation/suggestion I will just add that I
always use the suffix L to numbers so that I have integers whenever I need
them.  Here's an example:
> str(1)
 num 1
> str(1L)
 int 1
> str(c(1,2,3))
 num [1:3] 1 2 3
> str(c(1L,2L,3L))
 int [1:3] 1 2 3
>

This isn't possible when you're indexing based on values extracted from
other lists/vectors in which case you have to use `as.integer` to force
integers.  Again, I find `str` to be invaluable in figuring all this out.
HTH,
Vijay.

On Wed, Aug 7, 2019 at 6:50 PM Barry Rowlingson <b.rowlingson using gmail.com>
wrote:

> I recently answered a similar question on Stack Overflow where someone
> needed to add detached polygons to their connected network by connecting
> them to their nearest neighbour:
>
>
> https://stackoverflow.com/questions/57269254/how-to-impute-missing-neighbours-of-a-spatial-weight-matrix-queen-contiguity/57378930?noredirect=1#comment101246065_57378930
>
> in short, you can treat a `nb` object like a list of vectors: nb[[i]] is a
> vector of indexes of objects connected to object `i`
>
> BUT you have to make sure you store integers:
>
> Here's a `nb` object from that question which in summary has this
> manyneighbours for each region:
>
> > card(nb)
> [1] 2 3 4 3 2 0 0
>
> lets set the 6th feature to be a neighbour of the first:
>
> > nb[[6]] = 1
>
> then uh-oh...
>
> > card(nb)
> Error in card(nb) :
>   INTEGER() can only be applied to a 'integer', not a 'double'
>
> same again only `as.integer`:
>
> > nb[[6]] = as.integer(1)
>
> and its happy:
>
> > card(nb)
> [1] 2 3 4 3 2 1 0
>
> if you want to set the nighbours of 6 to several features:
>
> > nb[[6]] = as.integer(c(1,2,3))
> > card(nb)
> [1] 2 3 4 3 2 3 0
>
> Barry
>
>
>
> On Wed, Aug 7, 2019 at 10:25 PM Stuart Reece <stuart.reece using bigpond.com>
> wrote:
>
>> Thanks Vijay.
>>
>> Big help.
>>
>> I will go through the recommended chapter in detail.
>>
>> Normally for a list I can just use single square brackets like this [] to
>> access the elements and change them by assignment.
>>
>> But I cannot quite figure out how to do this with the nb lists.
>>
>>
>>
>> Doing this with [[]] works really well to create nicely corrected graphs.
>>
>>
>>
>> But fails due it “out of bounds index errors” with regression
>> equations????
>>
>>
>>
>> I find this ever so confusing….???
>>
>>
>>
>> Thankyou so much again,
>>
>>
>>
>> Stuart Reece.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> From: Vijay Lulla <vijaylulla using gmail.com>
>> Sent: Thursday, 8 August, 2019 1:53 AM
>> To: Stuart Reece <stuart.reece using bigpond.com>
>> Cc: Roger Bivand <Roger.Bivand using nhh.no>; R-sig-geo Mailing List <
>> r-sig-geo using r-project.org>
>> Subject: Re: [R-sig-Geo] Adding a Few Neighbour Relationships to a nb List
>>
>>
>>
>> Maybe
>> https://cran.r-project.org/web/packages/spdep/vignettes/nb_igraph.html
>> can help with all your questions.
>> https://cran.r-project.org/web/packages/spdep/vignettes/nb_sf.html
>> contains a little more detail about nb structure.  Finally, I encourage you
>> to use `str` to study the structure of R objects.
>>
>> HTH,
>>
>> Vijay.
>>
>>
>>
>> On Wed, Aug 7, 2019 at 10:44 AM Stuart Reece <stuart.reece using bigpond.com
>> <mailto:stuart.reece using bigpond.com> > wrote:
>>
>> Dear R-Sig-Geo list,
>>
>>
>>
>> I was wondering if it might be possible please to request assistance with
>> adding some nb relationships to a .nb.gal list composed either by GeoDa or
>> poly2nb in R????
>>
>>
>>
>> The shapefile at this URL
>> <
>> https://www.samhsa.gov/data/report/2014-2016-nsduh-substate-region-shapefil
>> e>  divides USA into 395 substate regions.  For health and demographical
>> reasons it is important to include both Hawaii and Alaska in the
>> spatiotemporal analysis so I want to introduce these into the Southeast
>> coast of California and the Pacific northwest respectively.
>>
>>
>>
>> This is just as Giovanni Millo added in spatial relationships for Sicily
>> across the Strait of Messina for splm on page 7 of the splm pdf.
>>
>>
>>
>> I found edit.nb in spdep and operated it just as described in the
>> instructions and here
>> <https://github.com/r-spatial/spdep/blob/master/man/edit.nb.Rd> .  It
>> crashed RStudio many times but ran well in R3.6.1.  However even though I
>> assigned it to a new object it did not save well.  Although when I plotted
>> the dxxx file as the difference between the old and modified files it
>> plotted the changes beautifully in red and black respectively when plotted
>> by themselves it introduced many long distance extraneous relationships.
>> To
>> get the edited nb list file out of R 3.6.1 and into RStudio I saved it as
>> an
>> RDS file.  However when opened in RStudio it was grossly erroneous and
>> included extraneous links from Hawaii to Boston and New York.  When I
>> opened
>> the file in RStudio it again introduced these extraneous links.
>>
>>
>>
>> Saving it as a further new object in R 3.6.1 did not remedy these
>> difficulties.
>>
>>
>>
>> The other problem I have is that the spdep poly2nb function excludes
>> Richmond, an island off the southern tip of Long Island near New York as
>> it
>> is an island.  Also one of the areas - Region 10 in Washington DC - is
>> also
>> excluded for reasons of which I am unsure.
>>
>>
>>
>> I found some code here to just patch single areas
>> <https://stat.ethz.ch/pipermail/r-sig-geo/2006-June/001073.html>  like
>> this
>> but when I run it, it throws an integer error
>>
>> "  INTEGER() can only be applied to a 'integer', not a 'double'
>>
>>
>>
>> No combination of bracketing around subscripts helps or works at all.  The
>> link mentioned has these statements in it
>>
>>
>>
>> nb[[ij[1]]] <- sort(unique(c(nb[[ij[1]]], ij[2])))
>> nb[[ij[2]]] <- sort(unique(c(nb[[ij[2]]], ij[1])))
>>
>>
>>
>> which makes me think that I should insert a vector "  c(i,j) "  where
>> indicated.  Even using "c(as.integer(i),as.integer(j)) "
>>
>> or " as.integer(c(I,j)) "   doesn't work and still gives rise to the same
>> error.
>>
>>
>>
>> I am sure I am not the only one to have encountered such difficulties but
>> I
>> have really tried everything I can think of.
>>
>>
>>
>> The other thing I would really like is some clear instructions as to the
>> true underlying structure of the nb list.  If I could clearly understand
>> this then I could just go into the affected lines of the list of lists and
>> edit them directly.
>>
>>
>>
>> However I am quite unable to find any clear description of its structure
>> on
>> line.
>>
>>
>>
>> Similarly I cannot find the source code for drop.links online to try to
>> translate this code into add.links directly, as was also suggested.
>>
>>
>>
>> But such a function would I think be enormously helpful and of invaluable
>> assistance for final editing.
>>
>>
>>
>> Thankyou ever so much in advance for your kind and gracious assistance.
>>
>>
>>
>> Yours sincerely,
>>
>>
>>
>> Stuart Reece.
>>
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo using r-project.org <mailto:R-sig-Geo using r-project.org>
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>
>>
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo using r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list