[R] Neighbour List to Matrix

David Winsemius dwinsemius at comcast.net
Fri Feb 17 20:53:46 CET 2012


On Feb 17, 2012, at 2:19 PM, A J wrote:

>
> I don't know why data are not well shown... I have attached a PDF  
> file to this message with the structure of the begining list and the  
> final matrix one.

You should have attached the sample.txt file.

> I think I have no explain the issue in a right way. Sorry for the  
> inconveniences. The last thing I want is to disturb with technical  
> questions...
> Best,
> AJ

nbrs <- read.table(text="ID1 IDP2 SUMVAL
  1 2 0.065
  1 3 0.044
  3 1 0.071
  3 4 0.016
  3 5 0.011
  4 3 0.004
  4 7 0.004
  4 9 0.004
  5 4 0.010
  5 6 0.011
  5 8 0.009
  5 9 0.004", header=TRUE)
  mnbrs <- matrix(NA, ncol=max(c(nbrs$ID1, nbrs$IDP2)),   
nrow=max(c(nbrs$ID1, nbrs$IDP2)))
  mnbrs[as.matrix(nbrs[,1:2]) ]<- nbrs$SUMVAL
  mnbrs
#--------------------
       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]
[1,]    NA 0.065 0.044    NA    NA    NA    NA    NA    NA
[2,]    NA    NA    NA    NA    NA    NA    NA    NA    NA
[3,] 0.071    NA    NA 0.016 0.011    NA    NA    NA    NA
[4,]    NA    NA 0.004    NA    NA    NA 0.004    NA 0.004
[5,]    NA    NA    NA 0.010    NA 0.011    NA 0.009 0.004
[6,]    NA    NA    NA    NA    NA    NA    NA    NA    NA
[7,]    NA    NA    NA    NA    NA    NA    NA    NA    NA
[8,]    NA    NA    NA    NA    NA    NA    NA    NA    NA
[9,]    NA    NA    NA    NA    NA    NA    NA    NA    NA

\\\\\\\\\\\\\\\
'
Then type:

  ?write.matrix

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list