Last data update: 2014.03.03

R: Convert nb and gra format into each other
Interface between nb and gra formatR Documentation

Convert nb and gra format into each other

Description

Convert neighborhood structure objects of class "nb" from R-package spdep to graph objects of class "gra" from R-package R2BayesX and vice versa.

Usage

nb2gra(nbObject)
gra2nb(graObject)

Arguments

nbObject

neighborhood structure object of class "nb"

graObject

graph object of class "gra"

Value

Equivalent object in the other format.

Author(s)

Daniel Sabanes Bove.

See Also

sp2bnd, bnd2sp for conversion between the geographical information formats and read.gra, write.gra for the interface to the R2BayesX files.

Examples

## first nb to gra:
if(require("spdep")) {
  example(columbus)
  colNb <- poly2nb(columbus)

  ## ... here manual editing is possible ...
  ## then export to graph format
  colGra <- nb2gra(colNb)

  ## and save in BayesX file
  graFile <- tempfile()
  write.gra(colGra, file=graFile)

  ## now back from gra to nb:
  colGra <- read.gra(graFile)
  newColNb <- gra2nb(colGra)
  newColNb

  ## compare this with the original
  colNb

  ## only the call attribute does not match (which is OK):
  all.equal(newColNb, colNb, check.attributes = FALSE)
  attr(newColNb, "call")
  attr(colNb, "call")
}

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(R2BayesX)
Loading required package: BayesXsrc
Loading required package: colorspace
Loading required package: mgcv
Loading required package: nlme
This is mgcv 1.8-12. For overview type 'help("mgcv-package")'.
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/R2BayesX/nbAndGraConversion.Rd_%03d_medium.png", width=480, height=480)
> ### Name: Interface between nb and gra format
> ### Title: Convert nb and gra format into each other
> ### Aliases: nb2gra gra2nb
> ### Keywords: spatial
> 
> ### ** Examples
> 
> ## first nb to gra:
> if(require("spdep")) {
+   example(columbus)
+   colNb <- poly2nb(columbus)
+ 
+   ## ... here manual editing is possible ...
+   ## then export to graph format
+   colGra <- nb2gra(colNb)
+ 
+   ## and save in BayesX file
+   graFile <- tempfile()
+   write.gra(colGra, file=graFile)
+ 
+   ## now back from gra to nb:
+   colGra <- read.gra(graFile)
+   newColNb <- gra2nb(colGra)
+   newColNb
+ 
+   ## compare this with the original
+   colNb
+ 
+   ## only the call attribute does not match (which is OK):
+   all.equal(newColNb, colNb, check.attributes = FALSE)
+   attr(newColNb, "call")
+   attr(colNb, "call")
+ }
Loading required package: spdep
Loading required package: sp
Loading required package: Matrix

colmbs> require(maptools)
Loading required package: maptools
Checking rgeos availability: TRUE

colmbs> columbus <- readShapePoly(system.file("etc/shapes/columbus.shp",
colmbs+  package="spdep")[1])

colmbs> col.gal.nb <- read.gal(system.file("etc/weights/columbus.gal",
colmbs+  package="spdep")[1])
Note: map consists of 49 regions
Creating adjacency matrix ...
finished
poly2nb(pl = columbus)
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>