Skip to content

Neighbour List to Matrix

7 messages · jim holtman, A J, David Winsemius +1 more

#
You might want to post a better example of what your data looks like.
In the email, it is hard to tell how to split the data into rows that
can be read with three fields since it looks like the data is composed
of pairs of numbers.
On Fri, Feb 17, 2012 at 7:42 AM, A J <anxusgo at hotmail.com> wrote:

  
    
#
Is this what you are after:
+ 1   56   0.065935895
+ 1   900   0.044110185
+ 1   1409   0.196314197
+ 3   4   0.071320388
+ 3   83   0.016269564
+ 3   529   0.011359239
+ 3   883   0.012242533
+ 3   1242   0.016558924
+ 4   3   0.004307024
+ 4   7   0.004699821
+ 4   16   0.004735112
+ 5   7   0.010324926
+ 5   16   0.011250504
+ 5   498   0.009709858
+ 5   502   0.004775749
+ 5   508   0.031411560", header = TRUE)
'data.frame':   16 obs. of  3 variables:
 $ ID1   : int  1 1 1 3 3 3 3 3 4 4 ...
 $ IDP2  : int  56 900 1409 4 83 529 883 1242 3 7 ...
 $ SUMVAL: num  0.0659 0.0441 0.1963 0.0713 0.0163 ...
num [1:16, 1:3] 1 1 1 3 3 3 3 3 4 4 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:3] "ID1" "IDP2" "SUMVAL"
ID1 IDP2     SUMVAL
[1,]   1   56 0.06593589
[2,]   1  900 0.04411019
[3,]   1 1409 0.19631420
[4,]   3    4 0.07132039
[5,]   3   83 0.01626956
[6,]   3  529 0.01135924

At this point you could use 'wrtie.csv' to create a CSV file.
On Fri, Feb 17, 2012 at 1:42 PM, A J <anxusgo at hotmail.com> wrote:

  
    
A J
#
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. 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
------------ pr?xima parte ------------
A non-text attachment was scrubbed...
Name: sample.pdf
Type: application/pdf
Size: 5356 bytes
Desc: no disponible
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120217/2dca10ee/attachment.pdf>
#
On Feb 17, 2012, at 2:19 PM, A J wrote:

            
You should have attached the sample.txt file.
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
#
I think is good to know that list contain more than 600000 rows with
around 14000 nodes (participants).

?read.table may be unreliable for large matrices and with 14/600
you'll end up with many NA's. You might do better with

nbrs<- scan('nbrs.txt',skip=1,what=list('integer','integer',double(0)))
names(nbrs) <- c('c1','c2','c3')
xtabs(c3~c1+c2,nbrs,sparse=T)

# returns
4 x 9 sparse Matrix of class "dgCMatrix"
      1     2     3     4     5     6     7     8     9
1 .     0.065 0.044 .     .     .     .     .     .
3 0.071 .     .     0.016 0.011 .     .     .     .
4 .     .     0.004 .     .     .     0.004 .     0.004
5 .     .     .     0.010 .     0.011 .     0.009 0.004

Cheers