[R] Neighbour List to Matrix

jim holtman jholtman at gmail.com
Fri Feb 17 19:49:40 CET 2012


Is this what you are after:

> x <- read.table(text = "ID1   IDP2   SUMVAL
+ 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)
>
> str(x)  # dataframe
'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 ...
> x.m <- as.matrix(x)
> str(x.m) # matrix
 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"
> head(x.m)
     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:
> Sorry too much. I was convinced it was able to view well. So, the structure
> of my data set is something like this:
>
>
> ID1   IDP2   SUMVAL
>
> 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
> ...
>
>
> Summarizing: I have all data recorded in a TXT file, and I would like, after
> loading in R, transform this list into a matrix (complete matrix, not half)
> in order to export to CSV or TXT finally.
> Thank you very much for everything.
> Best,
> AJ
>
>> Date: Fri, 17 Feb 2012 13:12:55 -0500
>> Subject: Re: [R] Neighbour List to Matrix
>> From: jholtman at gmail.com
>> To: anxusgo at hotmail.com
>> CC: r-help at r-project.org
>>
>> 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list