[R] make some possible neighbourhoods

Muhammad Subianto subianto at gmail.com
Fri Oct 7 17:12:11 CEST 2005


Dear all,
I want to make some possible neighbourhoods in dataset below,
   V1 <- c(0,1,2,3)
   V2 <- c(0,1)
   V3 <- c(0,1,2)
   V4 <- c(0,1,2,3,4)
and then I have a domain which the number of each variables.
For dataset above a domain,
   domains <- c(3,1,2,4)
To create the neighbourhoods I choice one point from all possible point 
in dataset. I take one random point, for instance:
   point <- c(2,1,0,0)
To produce the neighbourhoods I run like this (see a code below):
   neighb2(point,domains)
 >   neighb2(point,domains)
Error: subscript out of bounds
 >

Try one random point again,
   point2 <- c(2,0,1,3)
   neighb2(point2,domains)
 >   neighb2(point2,domains)
Error: subscript out of bounds
 >

but if I choice a random point,
 >  point1 <- c(0,1,1,0)
 >   neighb2(point1,domains)
       [,1] [,2] [,3] [,4]
  [1,]    1    0    1    0
  [2,]    1    2    1    0
  [3,]    1    3    1    0
  [4,]    2    0    1    0
  [5,]    2    2    1    0
...
[65,]    0    1    3    3
[66,]    0    1    4    3
[67,]    0    1    0    4
[68,]    0    1    2    4
[69,]    0    1    3    4
[70,]    0    1    4    4
 >
it's OK.

I am not sure where I am mistake.
I believe it is the problem of the code.
How should I fix this problem.
Thanks in advance for any help.

Best regards,
Muhammad Subianto

Here is a code:

neighb2 <- function(point,domains) {
   nn2 <- sum(domains)*(sum(domains)-1)
   nvar <- length(point)
   neighb <- matrix(nrow=nn2,ncol=nvar)
   k <- 1
   for (i in 1:nvar) {
        restvars <- 1:nvar
        restvars <- restvars[-i]
        for (j in restvars) {
             values1 <- values2 <- 0:domains[i]
             values1 <- values1[-(point[i]+1)]
             values2 <- values2[-(point[j]+1)]
             for (m in values1) {
                  for (n in values2) {
                       neigh <- point
                       neigh[i] <- m
                       neigh[j] <- n
                       neighb[k,] <- neigh
                       k <- k+1
                  }
             }
        }
   }
   unique(neighb)
   }




More information about the R-help mailing list