An embedded and charset-unspecified text was scrubbed... Name: no disponible URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120217/59ad1a1e/attachment.pl>
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:
Hi everybody!I'm a new user of R. I've been having a look to sotored mails in list, but I've been not able to find one suiting to my need. The issue is I have a neighbour list in a TXT file, just as lists generated by social network softwares (f.i. Pajek), where you get Field1, Field2, Value. In my case, list follow the next way:ID1 ?IDP2 ? ?SUMVAL1 56 ? ? ?0.0659358951 ? ?900 ? ? 0.0441101851 ? ?1409 ? ?0.1963141973 ? ?4 ? ? ? 0.0713203883 ? ?83 ? ? ?0.0162695643 ? ?529 ? ? 0.0113592393 ? ?883 ? ? 0.0122425333 ? ?1242 ? ?0.0165589244 ? ?3 ? ? ? 0.0043070244 ? ?7 ? ? ? 0.0046998214 ? ?16 ? ? ?0.0047351125 ? ?7 ? ? ? 0.0103249265 ? ?16 ? ? ?0.0112505045 ? ?498 ? ? 0.0097098585 ? ?502 ? ? 0.0047757495 ? ?508 ? ? 0.031411560 The question is, how can I convert this list into a matrix? I think is good to know that list contain more than 600000 rows with around 14000 nodes (participants). If it's possible I would like to know all necessary stetps to do this. This is: load the TXT file, create the matrix through data contained in it, and finally to export to CSV or TXT file. Any idea or suggestion will be wellcome!Thank you very much in advance.AJ ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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.
An embedded and charset-unspecified text was scrubbed... Name: no disponible URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120217/50edb35e/attachment.pl>
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.
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
Date: Fri, 17 Feb 2012 13:49:40 -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 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.
------------ 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:
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
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