# [R] Two problems

David A Richmond richmond at saintmarys.edu
Thu Apr 18 21:36:13 CEST 2002

```On Thu, 18 Apr 2002, Ambrosini Alessandro wrote:

> Hello! Two questions:
>
> 1: I have to import a matrix of adjacency from a file of a software that is
> not R (for example "bloc notes" of Windows). The problem is that the matrix
> is not in the explicit form as
>  0 1 1
>  1 0 0
>  1 0 0
> but it is a scattered matrix where in each row there are two nodes that have
> a direct path.
> The matrix is
>
>  a   b
>  a   c
>  b   a
>  c   a
>

after doing a little research, I have discovered "charmatch" which will
give the indices of the matches between two sets of strings. For instance:

> list1 <- c("a","b","c","d")
> list2 <- c("d","d","b")
> charmatch(list2,list1)
[1] 4 4 2

Using this you can convert a list of letters to a list of numbers. For
instance:

> rowlist <- c("a","a","b","c")
> collist <- c("b","c","a","a")
> uniquelist <- sort(unique(c(rowlist,collist)))
> uniquelist
[1] "a" "b" "c"
> rownums <- charmatch(rowlist,uniquelist)
> colnums <- charmatch(collist,uniquelist)
> list <- cbind(rownums,colnums)
> list
rownums colnums
[1,]       1       2
[2,]       1       3
[3,]       2       1
[4,]       3       1
> n <- length(uniquelist)
> mat <- matrix(0,n,n)
> mat[list] <- 1
> mat
[,1] [,2] [,3]
[1,]    0    1    1
[2,]    1    0    0
[3,]    1    0    0

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```