[R] help with one matrix

Gabor Grothendieck ggrothendieck at gmail.com
Sun Sep 11 08:23:19 CEST 2005


On 9/10/05, Jose Claudio Faria <joseclaudio.faria at terra.com.br> wrote:
> Dear R-list,
> 
> Could anybody tell me how to make one matrix as the below:
> 
>      [,1] [,2] [,3] [,4] [,5] [,6]
> [1,]    -    2    3    4    5    6
> [2,]    2    -    2    3    4    5
> [3,]    3    2    -    2    3    4
> [4,]    4    3    2    -    2    3
> [5,]    5    4    3    2    -    2
> [6,]    6    5    4    3    2    -
> 

Assuming that - means NA 

dd <- diag(NA, 6)
abs(col(dd) - row(dd)) + 1 + dd

or

abs(outer(1:6, 1:6, "-")) + 1 + diag(NA,6)

or

f <- function(x,y) ifelse(x==y, NA, abs(x-y)+1)
outer(1:6, 1:6, f)




More information about the R-help mailing list