# [R] make some possible neighbourhoods

Date: Sat 08 Oct 2005 - 01:12:11 EST

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,

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)
}

R-help@stat.math.ethz.ch mailing list