An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090529/9bd630d9/attachment-0001.pl>
maxtrix to permutation vector
7 messages · Ian Coe, Romain Francois, Stavros Macrakis +3 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090529/49d29ff5/attachment-0001.pl>
About this;
> m <- matrix( 1:9, nr = 3 )
> dimnames( m ) <- list( c("d", "e", "f"), c("a", "b", "c") )
> data.frame( row = rep( rownames(m), ncol(m)), col = rep(
colnames(m), each = nrow(m)), data = as.vector(m) )
row col data
1 d a 1
2 e a 2
3 f a 3
4 d b 4
5 e b 5
6 f b 6
7 d c 7
8 e c 8
9 f c 9
or
data.frame( do.call( expand.grid, dimnames(m) ), data = as.vector(m))
Some timings on my machine with a 700x700 matrix:
> m <- matrix( rnorm( 700*700 ), nr = 700 )
> dimnames( m ) <- rep( list( as.character( 1:700) ), 2 )
> system.time( data.frame( row = rep( rownames(m), ncol(m)), col = rep(
colnames(m), each = nrow(m)), data = as.vector(m) ) )
user system elapsed
0.468 0.101 0.594
> system.time( data.frame( do.call( expand.grid, dimnames(m) ), data =
as.vector(m)) )
user system elapsed
0.432 0.106 0.551
Romain
Ian Coe wrote:
Hi,
Is there a way to convert a matrix into a vector representing all
permutations of values and column/row headings with native R functions?
I did this with 2 nested for loops and it took about 25 minutes to run
on a ~700x700 matrix. I'm assuming there must be a smarter way to do
this with R's vector commands, but being new to R, I'm having trouble
making it work.
Thanks,
Ian
[a] [b] [c]
[d] 1 4 7
[e] 2 5 8
[f] 3 6 9
a d 1
a e 2
a f 3
b d 4
b e 5
b f 6
c d 7
c e 8
c f 9
Ian Coe
Connective Capital Management, LLC
385 Homer Ave.
Palo Alto, CA 94301
(650) 321-4826 ext. 03
CONFIDENTIALITY NOTICE: This e-mail communication (inclu...{{dropped:23}}
______________________________________________ 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.
Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090529/6cada69b/attachment-0001.pl>
Try this:
m <- matrix(1:9, 3, dimnames = list(R = letters[1:3], C = LETTERS[1:3])) as.data.frame.table(m, responseName = "Value")
R C Value 1 a A 1 2 b A 2 3 c A 3 4 a B 4 5 b B 5 6 c B 6 7 a C 7 8 b C 8 9 c C 9
On Fri, May 29, 2009 at 2:08 PM, Ian Coe <ICoe at connectcap.com> wrote:
Hi,
? Is there a way to ?convert a matrix into a vector representing all
permutations of values and column/row headings with native R functions?
I did this with 2 nested for loops and it took about 25 minutes to run
on a ?~700x700 matrix. ?I'm assuming there must be a smarter way to do
this with R's vector commands, but being new to R, I'm having trouble
making it work.
Thanks,
Ian
? ? [a] [b] [c]
[d] ? ?1 ? ?4 ? ?7
[e] ? ?2 ? ?5 ? ?8
[f] ? ?3 ? ?6 ? ?9
a d 1
a e 2
a f 3
b d 4
b e 5
b f 6
c d 7
c e 8
c f 9
Ian Coe
Connective Capital Management, LLC
385 Homer Ave.
Palo Alto, CA 94301
(650) 321-4826 ext. 03
CONFIDENTIALITY NOTICE: This e-mail communication (inclu...{{dropped:23}}
______________________________________________ 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.
On Fri, 2009-05-29 at 11:08 -0700, Ian Coe wrote:
Hi, Is there a way to convert a matrix into a vector representing all permutations of values and column/row headings with native R functions? I did this with 2 nested for loops and it took about 25 minutes to run on a ~700x700 matrix. I'm assuming there must be a smarter way to do this with R's vector commands, but being new to R, I'm having trouble making it work.
Here is one way:
mat <- matrix(1:9, ncol = 3)
colnames(mat) <- c("a","b","c")
rownames(mat) <- c("d","e","f")
rc <- expand.grid(rownames(mat), colnames(mat))
res <- data.frame(Row = rc$Var2, Col = rc$Var1,
+ Perm = as.vector(mat))
res
Row Col Perm 1 a d 1 2 a e 2 3 a f 3 4 b d 4 5 b e 5 6 b f 6 7 c d 7 8 c e 8 9 c f 9 And timings for something similar to your 700*700 problem:
mat <- matrix(seq_len(700*700), ncol = 700)
colnames(mat) <- as.character(1:700)
rownames(mat) <- as.character(1:700)
system.time({
+ rc <- expand.grid(rownames(mat), colnames(mat)) + res <- data.frame(Row = rc$Var2, Col = rc$Var1, + Perm = as.vector(mat)) + }) user system elapsed 0.631 0.028 0.690
head(res)
Row Col Perm 1 1 1 1 2 1 2 2 3 1 3 3 4 1 4 4 5 1 5 5 6 1 6 6 HTH G
Thanks,
Ian
[a] [b] [c]
[d] 1 4 7
[e] 2 5 8
[f] 3 6 9
a d 1
a e 2
a f 3
b d 4
b e 5
b f 6
c d 7
c e 8
c f 9
Ian Coe
Connective Capital Management, LLC
385 Homer Ave.
Palo Alto, CA 94301
(650) 321-4826 ext. 03
CONFIDENTIALITY NOTICE: This e-mail communication (inclu...{{dropped:23}}
______________________________________________ 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.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090529/6b37c756/attachment-0002.bin>
Ian Coe wrote:
Hi,
Is there a way to convert a matrix into a vector representing all
permutations of values and column/row headings with native R functions?
I did this with 2 nested for loops and it took about 25 minutes to run
on a ~700x700 matrix. I'm assuming there must be a smarter way to do
this with R's vector commands, but being new to R, I'm having trouble
making it work.
Thanks,
Ian
[a] [b] [c]
[d] 1 4 7
[e] 2 5 8
[f] 3 6 9
a d 1
a e 2
a f 3
b d 4
b e 5
b f 6
c d 7
c e 8
c f 9
Ian Coe
Connective Capital Management, LLC
385 Homer Ave.
Palo Alto, CA 94301
(650) 321-4826 ext. 03
CONFIDENTIALITY NOTICE: This e-mail communication (inclu...{{dropped:23}}
______________________________________________ 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.
Hi Ian, Here's one way you could do it (assuming ori.mat is the original matrix and ori.mat has labeled columns and rows):
a.matrix <- expand.grid(rownames(ori.mat),colnames(ori.mat)) final.answer <- cbind(a.matrix,ori.mat[seq_along(ori.mat)])
Cheers,
*Luc Villandr?* /Biostatistician McGill University Health Center - Montreal Children's Hospital Research Institute/