# [R] Spatial: number of independent components?

Nikhil Kaza nikhil.list at gmail.com
Mon Jun 21 02:43:06 CEST 2010

I am sure this  can be written in a much more elegant and faster code.
One way I can think of, is to modify cell2nb code and create a new
function that creates the neighbour lists of only cells that are not
0. These are best directed to R-sig-Geo list.

However, the following might work.

library(spdep)
library(igraph)

> x=matrix(c(0,1,0,0,0,
>          0,1,1,0,0,
>          0,0,0,0,0,
>          0,0,0,1,0,
>          0,0,0,1,0),nrow=5)

a <- nb2mat(cell2nb(nrow(x),ncol(x)), style="B", torus="TRUE")
plot(g)
clusters(g)

Nikhil
---

Nikhil Kaza
Asst. Professor,
City and Regional Planning
University of North Carolina

nikhil.list at gmail.com

On Jun 20, 2010, at 7:17 PM, Daniel Malter wrote:

>
> Hi all, I am sorry if this is a very basic quesion, but I have no
> experience
> with analyzing spatial data and could not find the right function/
> package
> quickly. Any hints would be much appreciated. I have a matrix of
> spatial
> point patterns like the one below and want to find the number of
> independent
> components (if that's the right term) in that matrix (or in that
> image).
>
> x=matrix(c(0,1,0,0,0,
>          0,1,1,0,0,
>          0,0,0,0,0,
>          0,0,0,1,0,
>          0,0,0,1,0),nrow=5)
>
> image(x)
>
> I can find the number of populated points easily
>
> table(x)  #or more generally
> sum(x!=0)
>
> But I want to find the number of independent components. The answer
> in this
> example should be 2. There are three criteria to the function I am
> seeking:
>
> 1. Points that have a neighboring nonzero point should be counted as
> one
> contiguous component.
>
> 2. The function should respect that the matrix is projected on a
> torso. That
> is, points in the leftmost column border points in the rightmost
> column and
> points in the top row border points in the bottom row (if they are
> contiguous when you wrap the image around a cylinder).
>
> 3. The function should be fast/efficient since I need to run this over
> thousands of images/matrices.
>
> Is anyone aware of an implementation of such a function?
>
> Thanks much for your help,
> Daniel
> --
> View this message in context: http://r.789695.n4.nabble.com/Spatial-number-of-independent-components-tp2262018p2262018.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help