[R] Converting a Matrix to a Vector
Say I have: set.seed( 1 ) m - matrix( runif(5^2), nrow=5, dimnames = list( c(A,B,C,D,E), c(O,P,Q,R,S) ) ) m O P Q R S A 0.2655087 0.89838968 0.2059746 0.4976992 0.9347052 B 0.3721239 0.94467527 0.1765568 0.7176185 0.2121425 C 0.5728534 0.66079779 0.6870228 0.9919061 0.6516738 D 0.9082078 0.62911404 0.3841037 0.3800352 0.121 E 0.2016819 0.06178627 0.7698414 0.7774452 0.2672207 --- I want to create a vector v from matrix m that looks like this: A.O 0.2655087 B.O 0.3721239 v - as.vector( m ) almost gives me what I want, but then I need to take combinations of colnames( m ) and rownames( m ) to get my labels and hope they match up in order: if not, manipulate the order. This approach feels kludgy... Is this the right approach or is there a better way? -- View this message in context: http://www.nabble.com/Converting-a-Matrix-to-a-Vector-tp22696267p22696267.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Converting a Matrix to a Vector
try this: set.seed(1) m - matrix(runif(5^2), nrow = 5, dimnames = list(c(A,B,C,D,E), c(O,P,Q,R,S))) v - c(m) names(v) - paste(rownames(m), colnames(m)[col(m)], sep = .) # or # names(v) - outer(rownames(m), colnames(m), paste, sep = .) v I hope it helps. Best, Dimitris Ken-JP wrote: Say I have: set.seed( 1 ) m - matrix( runif(5^2), nrow=5, dimnames = list( c(A,B,C,D,E), c(O,P,Q,R,S) ) ) m O P Q R S A 0.2655087 0.89838968 0.2059746 0.4976992 0.9347052 B 0.3721239 0.94467527 0.1765568 0.7176185 0.2121425 C 0.5728534 0.66079779 0.6870228 0.9919061 0.6516738 D 0.9082078 0.62911404 0.3841037 0.3800352 0.121 E 0.2016819 0.06178627 0.7698414 0.7774452 0.2672207 --- I want to create a vector v from matrix m that looks like this: A.O 0.2655087 B.O 0.3721239 v - as.vector( m ) almost gives me what I want, but then I need to take combinations of colnames( m ) and rownames( m ) to get my labels and hope they match up in order: if not, manipulate the order. This approach feels kludgy... Is this the right approach or is there a better way? -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ R-help@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.
Re: [R] Converting a Matrix to a Vector
G'day Ken, On Wed, 25 Mar 2009 00:13:48 -0700 (PDT) Ken-JP kfmf...@gmail.com wrote: Say I have: set.seed( 1 ) m - matrix( runif(5^2), nrow=5, dimnames = list( c(A,B,C,D,E), c(O,P,Q,R,S) ) ) m O P Q R S A 0.2655087 0.89838968 0.2059746 0.4976992 0.9347052 B 0.3721239 0.94467527 0.1765568 0.7176185 0.2121425 C 0.5728534 0.66079779 0.6870228 0.9919061 0.6516738 D 0.9082078 0.62911404 0.3841037 0.3800352 0.121 E 0.2016819 0.06178627 0.7698414 0.7774452 0.2672207 --- I want to create a vector v from matrix m that looks like this: A.O 0.2655087 B.O 0.3721239 v - as.vector( m ) almost gives me what I want, but then I need to take combinations of colnames( m ) and rownames( m ) to get my labels and hope they match up in order: if not, manipulate the order. This approach feels kludgy... Is this the right approach or is there a better way? R tt - reshape(data.frame(m), direction=long, varying=list(1:5), ids=rownames(m), times=colnames(m)) R ind - names(tt) %in% c(time, id) R uu - tt[,!ind] R names(uu) - rownames(tt) R uu A.OB.OC.OD.OE.OA.PB.P 0.26550866 0.37212390 0.57285336 0.90820779 0.20168193 0.89838968 0.94467527 C.PD.PE.PA.QB.QC.QD.Q 0.66079779 0.62911404 0.06178627 0.20597457 0.17655675 0.68702285 0.38410372 E.QA.RB.RC.RD.RE.RA.S 0.76984142 0.49769924 0.71761851 0.99190609 0.38003518 0.77744522 0.93470523 B.SC.SD.SE.S 0.21214252 0.65167377 0.1210 0.26722067 HTH. Cheers, Berwin === Full address = Berwin A TurlachTel.: +65 6516 4416 (secr) Dept of Statistics and Applied Probability+65 6516 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: sta...@nus.edu.sg Singapore 117546http://www.stat.nus.edu.sg/~statba __ R-help@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.
Re: [R] Converting a Matrix to a Vector
Use the 'reshape' package: library(reshape) melt(m) X1 X2 value 1 A O 0.26550866 2 B O 0.37212390 3 C O 0.57285336 4 D O 0.90820779 5 E O 0.20168193 6 A P 0.89838968 7 B P 0.94467527 8 C P 0.66079779 9 D P 0.62911404 10 E P 0.06178627 11 A Q 0.20597457 12 B Q 0.17655675 13 C Q 0.68702285 14 D Q 0.38410372 15 E Q 0.76984142 16 A R 0.49769924 17 B R 0.71761851 18 C R 0.99190609 19 D R 0.38003518 20 E R 0.77744522 21 A S 0.93470523 22 B S 0.21214252 23 C S 0.65167377 24 D S 0.1210 25 E S 0.26722067 On Wed, Mar 25, 2009 at 3:13 AM, Ken-JP kfmf...@gmail.com wrote: Say I have: set.seed( 1 ) m - matrix( runif(5^2), nrow=5, dimnames = list( c(A,B,C,D,E), c(O,P,Q,R,S) ) ) m O P Q R S A 0.2655087 0.89838968 0.2059746 0.4976992 0.9347052 B 0.3721239 0.94467527 0.1765568 0.7176185 0.2121425 C 0.5728534 0.66079779 0.6870228 0.9919061 0.6516738 D 0.9082078 0.62911404 0.3841037 0.3800352 0.121 E 0.2016819 0.06178627 0.7698414 0.7774452 0.2672207 --- I want to create a vector v from matrix m that looks like this: A.O 0.2655087 B.O 0.3721239 v - as.vector( m ) almost gives me what I want, but then I need to take combinations of colnames( m ) and rownames( m ) to get my labels and hope they match up in order: if not, manipulate the order. This approach feels kludgy... Is this the right approach or is there a better way? -- View this message in context: http://www.nabble.com/Converting-a-Matrix-to-a-Vector-tp22696267p22696267.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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 Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ R-help@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.
Re: [R] Converting a Matrix to a Vector
try also m - matrix( runif(5^2), nrow=5, dimnames = Names- list( c(A,B,C,D,E), c(O,P,Q,R,S) ) ) data.frame(expand.grid(Names[[1]],Names[[2]]),as.numeric(m)) data.frame(code=outer(Names[[1]],Names[[2]],paste,sep=.)[1:25],num=as.numeric(m)) Patrizio 2009/3/25 jim holtman jholt...@gmail.com: Use the 'reshape' package: library(reshape) melt(m) X1 X2 value 1 A O 0.26550866 2 B O 0.37212390 3 C O 0.57285336 4 D O 0.90820779 5 E O 0.20168193 6 A P 0.89838968 7 B P 0.94467527 8 C P 0.66079779 9 D P 0.62911404 10 E P 0.06178627 11 A Q 0.20597457 12 B Q 0.17655675 13 C Q 0.68702285 14 D Q 0.38410372 15 E Q 0.76984142 16 A R 0.49769924 17 B R 0.71761851 18 C R 0.99190609 19 D R 0.38003518 20 E R 0.77744522 21 A S 0.93470523 22 B S 0.21214252 23 C S 0.65167377 24 D S 0.1210 25 E S 0.26722067 On Wed, Mar 25, 2009 at 3:13 AM, Ken-JP kfmf...@gmail.com wrote: Say I have: set.seed( 1 ) m - matrix( runif(5^2), nrow=5, dimnames = list( c(A,B,C,D,E), c(O,P,Q,R,S) ) ) m O P Q R S A 0.2655087 0.89838968 0.2059746 0.4976992 0.9347052 B 0.3721239 0.94467527 0.1765568 0.7176185 0.2121425 C 0.5728534 0.66079779 0.6870228 0.9919061 0.6516738 D 0.9082078 0.62911404 0.3841037 0.3800352 0.121 E 0.2016819 0.06178627 0.7698414 0.7774452 0.2672207 --- I want to create a vector v from matrix m that looks like this: A.O 0.2655087 B.O 0.3721239 v - as.vector( m ) almost gives me what I want, but then I need to take combinations of colnames( m ) and rownames( m ) to get my labels and hope they match up in order: if not, manipulate the order. This approach feels kludgy... Is this the right approach or is there a better way? -- View this message in context: http://www.nabble.com/Converting-a-Matrix-to-a-Vector-tp22696267p22696267.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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 Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ R-help@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. __ R-help@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.