Re: [R] matrix into vector with vertex names - shortestpath reshape2
Ok, the problem seems simpler than I thought. It works well if omit two lines: y_s[,1] - V(y)$name[y_s[,1]] y_s[,2] - V(y)$name[y_s[,2]] It seems melt creates a data.frame (unlike what it did two years ago?) Joe On Tue, Aug 27, 2013 at 12:35 AM, Joseph J. Bakker joe.st...@gmail.comwrote: Two years ago, as shown in the script at the end, I created shortestpaths using Igraph, then using Melt in Reshape2 I converted the the resulting matrix into three column vectors - vertex1, vertex2, shortestpath. It worked then. However, in the meantime I have installed new versions of R and these two libraries. Creating column vectors with names of rows and columns don't work anymore; the name columns are mostly blank. Could anyone suggest a solution, preferably not dependent on a library, given my bad experience with Reshape2? Below is the original code #code for generating shortest path matrix and creating a 3 columns from an igraph graph object y y_s-shortest.paths(y, weights = NULL) y_s - melt(y_s)[c(upper.tri(y_s)),] y_s[,1] - V(y)$name[y_s[,1]] y_s[,2] - V(y)$name[y_s[,2]] names(y_s)-c(vertex1, vertex2, shortestpath) Best regards, Joe. [[alternative HTML version deleted]] __ 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] matrix into vector with vertex names - shortestpath reshape2
Two years ago, as shown in the script at the end, I created shortestpaths using Igraph, then using Melt in Reshape2 I converted the the resulting matrix into three column vectors - vertex1, vertex2, shortestpath. It worked then. However, in the meantime I have installed new versions of R and these two libraries. Creating column vectors with names of rows and columns don't work anymore; the name columns are mostly blank. Could anyone suggest a solution, preferably not dependent on a library, given my bad experience with Reshape2? Below is the original code #code for generating shortest path matrix and creating a 3 columns from an igraph graph object y y_s-shortest.paths(y, weights = NULL) y_s - melt(y_s)[c(upper.tri(y_s)),] y_s[,1] - V(y)$name[y_s[,1]] y_s[,2] - V(y)$name[y_s[,2]] names(y_s)-c(vertex1, vertex2, shortestpath) Best regards, Joe. [[alternative HTML version deleted]] __ 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] matrix into vector with vertex names
Hi Gabor, Thanks. I will try to figure out the solution you suggest. I found out about melt() from a discussion forum; it seems to me that melt()$value is similar to c(), and when I modified the script as below it 'seems' to be running faster. Anyway in the end I only needed to use a smaller network so the computation went ok. y_s - melt(y_s)[c(upper.tri(y_s)),] #new code y_s - melt(y_s)[melt(upper.tri(y_s))$value,] #old code The reason I needed three columns was for later statistical analysis for which such a format was necessary. Best regards, Joe. On Thu, Aug 18, 2011 at 12:47 PM, Gábor Csárdi csa...@rmki.kfki.hu wrote: Joe, what is melt() supposed to do here? What's wrong with the simple solution of creating a data.frame first, and then filling it with values through a loop? Actually, keeping the matrix is just as good, indexing is just as fast, and takes the same amount of memory as your three column matrix, doesn't it? Gabor On Fri, Aug 5, 2011 at 10:40 AM, joe j joe.st...@gmail.com wrote: Using Igraph, I create shortest paths, then convert the matrix into three column vectors - vertex1, vertex2, shortestpath - as the code below shows. #code for generating shortest path matrix and creating a 3 columns from an igraph graph object y y_s-shortest.paths(y, weights = NULL) y_s - melt(y_s)[melt(upper.tri(y_s))$value,] #Step 2: this is where the trouble with memory occurs y_s[,1] - V(y)$name[y_s[,1]] y_s[,2] - V(y)$name[y_s[,2]] names(y_s)-c(vertex1, vertex2, shortestpath) However I am looking for an alternative way of doing this becase at the second step I run into a fight with my machine's memory. I know I can create vectors using as.vector(), c(), etc, but I am not able to create the two other columns with vertex names. Best regards, Joe. __ 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. -- Gabor Csardi csa...@rmki.kfki.hu MTA KFKI RMKI __ 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] matrix into vector with vertex names
Joe, what is melt() supposed to do here? What's wrong with the simple solution of creating a data.frame first, and then filling it with values through a loop? Actually, keeping the matrix is just as good, indexing is just as fast, and takes the same amount of memory as your three column matrix, doesn't it? Gabor On Fri, Aug 5, 2011 at 10:40 AM, joe j joe.st...@gmail.com wrote: Using Igraph, I create shortest paths, then convert the matrix into three column vectors - vertex1, vertex2, shortestpath - as the code below shows. #code for generating shortest path matrix and creating a 3 columns from an igraph graph object y y_s-shortest.paths(y, weights = NULL) y_s - melt(y_s)[melt(upper.tri(y_s))$value,] #Step 2: this is where the trouble with memory occurs y_s[,1] - V(y)$name[y_s[,1]] y_s[,2] - V(y)$name[y_s[,2]] names(y_s)-c(vertex1, vertex2, shortestpath) However I am looking for an alternative way of doing this becase at the second step I run into a fight with my machine's memory. I know I can create vectors using as.vector(), c(), etc, but I am not able to create the two other columns with vertex names. Best regards, Joe. __ 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. -- Gabor Csardi csa...@rmki.kfki.hu MTA KFKI RMKI __ 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] matrix into vector with vertex names
Using Igraph, I create shortest paths, then convert the matrix into three column vectors - vertex1, vertex2, shortestpath - as the code below shows. #code for generating shortest path matrix and creating a 3 columns from an igraph graph object y y_s-shortest.paths(y, weights = NULL) y_s - melt(y_s)[melt(upper.tri(y_s))$value,] #Step 2: this is where the trouble with memory occurs y_s[,1] - V(y)$name[y_s[,1]] y_s[,2] - V(y)$name[y_s[,2]] names(y_s)-c(vertex1, vertex2, shortestpath) However I am looking for an alternative way of doing this becase at the second step I run into a fight with my machine's memory. I know I can create vectors using as.vector(), c(), etc, but I am not able to create the two other columns with vertex names. Best regards, Joe. __ 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] Matrix to Vector
Hi All, I am trying to turn a Matrix into a vector for analysis purposes. I need to select only certain columns from the entire matrix for the vector (intraday time intervals). Also I need to transpose the Matrix (so times are in rows) stack each successive new column on top of each other (latest at the top creating one long vector vs a matrix) then reassign the date to the left of each time (right now date is only shown once by row and will be in columns when transposed). Any thoughts? [[alternative HTML version deleted]] __ 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] Matrix to Vector
How about: ?subset ?t ?as.vector I'm not sure transposing is really needed, but without a workable example as requested in the posting guide (hint, hint), it's hard to say for certain. Sarah On Tue, Jun 15, 2010 at 1:51 PM, George Coyle gcoy...@gmail.com wrote: Hi All, I am trying to turn a Matrix into a vector for analysis purposes. I need to select only certain columns from the entire matrix for the vector (intraday time intervals). Also I need to transpose the Matrix (so times are in rows) stack each successive new column on top of each other (latest at the top creating one long vector vs a matrix) then reassign the date to the left of each time (right now date is only shown once by row and will be in columns when transposed). Any thoughts? -- Sarah Goslee http://www.functionaldiversity.org __ 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] Matrix to Vector
George Coyle wrote: Hi All, I am trying to turn a Matrix into a vector for analysis purposes. I need to select only certain columns from the entire matrix for the vector (intraday time intervals). Also I need to transpose the Matrix (so times are in rows) stack each successive new column on top of each other (latest at the top creating one long vector vs a matrix) then reassign the date to the left of each time (right now date is only shown once by row and will be in columns when transposed). Any thoughts? Yes, specify your problem with reproducible R code, so we can help you. __ 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] Matrix to Vector
Given a matrix of m*n, I want to reorder it as a vector, using a row major transpose. so: m-matrix(seq(1,48),nrow=6,byrow=T) m [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,]12345678 [2,]9 10 11 12 13 14 15 16 [3,] 17 18 19 20 21 22 23 24 [4,] 25 26 27 28 29 30 31 32 [5,] 33 34 35 36 37 38 39 40 [6,] 41 42 43 44 45 46 47 48 I want to reorder this as a vector copying by row, so that the final vector has elements ordered thusly: row 1, column 1:N (m[1,1:n]) maps to row 1-n, and m[2,1:n] maps to row[n+1:2n] ... this obviously is not a solution: as the inherent column major storage paradigm of a matrix defeats the approach. dim(m)-c(48,1) m [,1] [1,]1 [2,]9 [3,] 17 [4,] 25 [5,] 33 [6,] 41 [7,]2 [8,] 10 [9,] 18 [10,] 26 [11,] 34 [12,] 42 [13,]3 [14,] 11 [15,] 19 [16,] 27 [17,] 35 [18,] 43 [19,]4 [20,] 12 [21,] 20 [22,] 28 [23,] 36 [24,] 44 [25,]5 [26,] 13 [27,] 21 [28,] 29 [29,] 37 [30,] 45 [31,]6 [32,] 14 [33,] 22 [34,] 30 [35,] 38 [36,] 46 [37,]7 [38,] 15 [39,] 23 [40,] 31 [41,] 39 [42,] 47 [43,]8 [44,] 16 [45,] 24 [46,] 32 [47,] 40 [48,] 48 I already have a version that loops through the data ( this is actually a portion of a data frame ) to reorder this into a vector, but I was hoping there was an elegant way [[alternative HTML version deleted]] __ 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] Matrix to Vector
Try this: as.vector(t(m)) On Sat, Jun 5, 2010 at 3:12 PM, steven mosher mosherste...@gmail.comwrote: Given a matrix of m*n, I want to reorder it as a vector, using a row major transpose. so: m-matrix(seq(1,48),nrow=6,byrow=T) m [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,]12345678 [2,]9 10 11 12 13 14 15 16 [3,] 17 18 19 20 21 22 23 24 [4,] 25 26 27 28 29 30 31 32 [5,] 33 34 35 36 37 38 39 40 [6,] 41 42 43 44 45 46 47 48 I want to reorder this as a vector copying by row, so that the final vector has elements ordered thusly: row 1, column 1:N (m[1,1:n]) maps to row 1-n, and m[2,1:n] maps to row[n+1:2n] ... this obviously is not a solution: as the inherent column major storage paradigm of a matrix defeats the approach. dim(m)-c(48,1) m [,1] [1,]1 [2,]9 [3,] 17 [4,] 25 [5,] 33 [6,] 41 [7,]2 [8,] 10 [9,] 18 [10,] 26 [11,] 34 [12,] 42 [13,]3 [14,] 11 [15,] 19 [16,] 27 [17,] 35 [18,] 43 [19,]4 [20,] 12 [21,] 20 [22,] 28 [23,] 36 [24,] 44 [25,]5 [26,] 13 [27,] 21 [28,] 29 [29,] 37 [30,] 45 [31,]6 [32,] 14 [33,] 22 [34,] 30 [35,] 38 [36,] 46 [37,]7 [38,] 15 [39,] 23 [40,] 31 [41,] 39 [42,] 47 [43,]8 [44,] 16 [45,] 24 [46,] 32 [47,] 40 [48,] 48 I already have a version that loops through the data ( this is actually a portion of a data frame ) to reorder this into a vector, but I was hoping there was an elegant way [[alternative HTML version deleted]] __ 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] __ 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] Matrix to Vector
Hi Steven, If I understood correctly, this might do what you want: c(t(m)) HTH, Jorge On Sat, Jun 5, 2010 at 2:12 PM, steven mosher mosherste...@gmail.comwrote: Given a matrix of m*n, I want to reorder it as a vector, using a row major transpose. so: m-matrix(seq(1,48),nrow=6,byrow=T) m [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,]12345678 [2,]9 10 11 12 13 14 15 16 [3,] 17 18 19 20 21 22 23 24 [4,] 25 26 27 28 29 30 31 32 [5,] 33 34 35 36 37 38 39 40 [6,] 41 42 43 44 45 46 47 48 I want to reorder this as a vector copying by row, so that the final vector has elements ordered thusly: row 1, column 1:N (m[1,1:n]) maps to row 1-n, and m[2,1:n] maps to row[n+1:2n] ... this obviously is not a solution: as the inherent column major storage paradigm of a matrix defeats the approach. dim(m)-c(48,1) m [,1] [1,]1 [2,]9 [3,] 17 [4,] 25 [5,] 33 [6,] 41 [7,]2 [8,] 10 [9,] 18 [10,] 26 [11,] 34 [12,] 42 [13,]3 [14,] 11 [15,] 19 [16,] 27 [17,] 35 [18,] 43 [19,]4 [20,] 12 [21,] 20 [22,] 28 [23,] 36 [24,] 44 [25,]5 [26,] 13 [27,] 21 [28,] 29 [29,] 37 [30,] 45 [31,]6 [32,] 14 [33,] 22 [34,] 30 [35,] 38 [36,] 46 [37,]7 [38,] 15 [39,] 23 [40,] 31 [41,] 39 [42,] 47 [43,]8 [44,] 16 [45,] 24 [46,] 32 [47,] 40 [48,] 48 I already have a version that loops through the data ( this is actually a portion of a data frame ) to reorder this into a vector, but I was hoping there was an elegant way [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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] Matrix to Vector
as.vector(t(m)) [1] 1 9 17 25 33 41 2 10 18 26 34 42 3 11 19 27 35 43 4 12 20 28 36 44 5 13 21 29 37 45 6 14 22 30 38 46 7 15 23 31 39 47 8 16 24 [46] 32 40 48 the result I want is this: [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 [46] 46 47 48 On Sat, Jun 5, 2010 at 11:17 AM, Henrique Dallazuanna www...@gmail.comwrote: Try this: as.vector(t(m)) On Sat, Jun 5, 2010 at 3:12 PM, steven mosher mosherste...@gmail.comwrote: Given a matrix of m*n, I want to reorder it as a vector, using a row major transpose. so: m-matrix(seq(1,48),nrow=6,byrow=T) m [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,]12345678 [2,]9 10 11 12 13 14 15 16 [3,] 17 18 19 20 21 22 23 24 [4,] 25 26 27 28 29 30 31 32 [5,] 33 34 35 36 37 38 39 40 [6,] 41 42 43 44 45 46 47 48 I want to reorder this as a vector copying by row, so that the final vector has elements ordered thusly: row 1, column 1:N (m[1,1:n]) maps to row 1-n, and m[2,1:n] maps to row[n+1:2n] ... this obviously is not a solution: as the inherent column major storage paradigm of a matrix defeats the approach. dim(m)-c(48,1) m [,1] [1,]1 [2,]9 [3,] 17 [4,] 25 [5,] 33 [6,] 41 [7,]2 [8,] 10 [9,] 18 [10,] 26 [11,] 34 [12,] 42 [13,]3 [14,] 11 [15,] 19 [16,] 27 [17,] 35 [18,] 43 [19,]4 [20,] 12 [21,] 20 [22,] 28 [23,] 36 [24,] 44 [25,]5 [26,] 13 [27,] 21 [28,] 29 [29,] 37 [30,] 45 [31,]6 [32,] 14 [33,] 22 [34,] 30 [35,] 38 [36,] 46 [37,]7 [38,] 15 [39,] 23 [40,] 31 [41,] 39 [42,] 47 [43,]8 [44,] 16 [45,] 24 [46,] 32 [47,] 40 [48,] 48 I already have a version that loops through the data ( this is actually a portion of a data frame ) to reorder this into a vector, but I was hoping there was an elegant way [[alternative HTML version deleted]] __ 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] __ 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] Matrix to Vector
m-matrix(seq(1,48),nrow=6,byrow=T) as.vector(t(m)) gives me the correct result. Any chance you may have already transformed m ? --- On Sat, 6/5/10, steven mosher mosherste...@gmail.com wrote: From: steven mosher mosherste...@gmail.com Subject: Re: [R] Matrix to Vector To: Henrique Dallazuanna www...@gmail.com Cc: r-help@r-project.org Received: Saturday, June 5, 2010, 2:44 PM as.vector(t(m)) [1] 1 9 17 25 33 41 2 10 18 26 34 42 3 11 19 27 35 43 4 12 20 28 36 44 5 13 21 29 37 45 6 14 22 30 38 46 7 15 23 31 39 47 8 16 24 [46] 32 40 48 the result I want is this: [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 [46] 46 47 48 On Sat, Jun 5, 2010 at 11:17 AM, Henrique Dallazuanna www...@gmail.comwrote: Try this: as.vector(t(m)) On Sat, Jun 5, 2010 at 3:12 PM, steven mosher mosherste...@gmail.comwrote: Given a matrix of m*n, I want to reorder it as a vector, using a row major transpose. so: m-matrix(seq(1,48),nrow=6,byrow=T) m [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1 2 3 4 5 6 7 8 [2,] 9 10 11 12 13 14 15 16 [3,] 17 18 19 20 21 22 23 24 [4,] 25 26 27 28 29 30 31 32 [5,] 33 34 35 36 37 38 39 40 [6,] 41 42 43 44 45 46 47 48 I want to reorder this as a vector copying by row, so that the final vector has elements ordered thusly: row 1, column 1:N (m[1,1:n]) maps to row 1-n, and m[2,1:n] maps to row[n+1:2n] ... this obviously is not a solution: as the inherent column major storage paradigm of a matrix defeats the approach. dim(m)-c(48,1) m [,1] [1,] 1 [2,] 9 [3,] 17 [4,] 25 [5,] 33 [6,] 41 [7,] 2 [8,] 10 [9,] 18 [10,] 26 [11,] 34 [12,] 42 [13,] 3 [14,] 11 [15,] 19 [16,] 27 [17,] 35 [18,] 43 [19,] 4 [20,] 12 [21,] 20 [22,] 28 [23,] 36 [24,] 44 [25,] 5 [26,] 13 [27,] 21 [28,] 29 [29,] 37 [30,] 45 [31,] 6 [32,] 14 [33,] 22 [34,] 30 [35,] 38 [36,] 46 [37,] 7 [38,] 15 [39,] 23 [40,] 31 [41,] 39 [42,] 47 [43,] 8 [44,] 16 [45,] 24 [46,] 32 [47,] 40 [48,] 48 I already have a version that loops through the data ( this is actually a portion of a data frame ) to reorder this into a vector, but I was hoping there was an elegant way [[alternative HTML version deleted]] __ 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] -Inline Attachment Follows- __ 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.
Re: [R] Matrix to Vector
I bet that is what I did. On Sat, Jun 5, 2010 at 11:54 AM, John Kane jrkrid...@yahoo.ca wrote: m-matrix(seq(1,48),nrow=6,byrow=T) as.vector(t(m)) gives me the correct result. Any chance you may have already transformed m ? --- On Sat, 6/5/10, steven mosher mosherste...@gmail.com wrote: From: steven mosher mosherste...@gmail.com Subject: Re: [R] Matrix to Vector To: Henrique Dallazuanna www...@gmail.com Cc: r-help@r-project.org Received: Saturday, June 5, 2010, 2:44 PM as.vector(t(m)) [1] 1 9 17 25 33 41 2 10 18 26 34 42 3 11 19 27 35 43 4 12 20 28 36 44 5 13 21 29 37 45 6 14 22 30 38 46 7 15 23 31 39 47 8 16 24 [46] 32 40 48 the result I want is this: [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 [46] 46 47 48 On Sat, Jun 5, 2010 at 11:17 AM, Henrique Dallazuanna www...@gmail.comwrote: Try this: as.vector(t(m)) On Sat, Jun 5, 2010 at 3:12 PM, steven mosher mosherste...@gmail.com wrote: Given a matrix of m*n, I want to reorder it as a vector, using a row major transpose. so: m-matrix(seq(1,48),nrow=6,byrow=T) m [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,]12 3456 78 [2,] 9 10 11 12 13 14 15 16 [3,] 17 18 19 20 21 22 23 24 [4,] 25 26 27 28 29 30 31 32 [5,] 33 34 35 36 37 38 39 40 [6,] 41 42 43 44 45 46 47 48 I want to reorder this as a vector copying by row, so that the final vector has elements ordered thusly: row 1, column 1:N (m[1,1:n]) maps to row 1-n, and m[2,1:n] maps to row[n+1:2n] ... this obviously is not a solution: as the inherent column major storage paradigm of a matrix defeats the approach. dim(m)-c(48,1) m [,1] [1,]1 [2,]9 [3,] 17 [4,] 25 [5,] 33 [6,] 41 [7,]2 [8,] 10 [9,] 18 [10,] 26 [11,] 34 [12,] 42 [13,]3 [14,] 11 [15,] 19 [16,] 27 [17,] 35 [18,] 43 [19,]4 [20,] 12 [21,] 20 [22,] 28 [23,] 36 [24,] 44 [25,]5 [26,] 13 [27,] 21 [28,] 29 [29,] 37 [30,] 45 [31,]6 [32,] 14 [33,] 22 [34,] 30 [35,] 38 [36,] 46 [37,]7 [38,] 15 [39,] 23 [40,] 31 [41,] 39 [42,] 47 [43,]8 [44,] 16 [45,] 24 [46,] 32 [47,] 40 [48,] 48 I already have a version that loops through the data ( this is actually a portion of a data frame ) to reorder this into a vector, but I was hoping there was an elegant way [[alternative HTML version deleted]] __ 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] -Inline Attachment Follows- __ 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. [[alternative HTML version deleted]] __ 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] matrix to vector
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]147 [e]258 [f]369 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@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] matrix to vector
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]147 [e]258 [f]369 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@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] matrix to vector
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. cbind(expand.grid(rownames(x),colnames(x)),as.vector(x)) Var1 Var2 as.vector(x) 1da1 2ea2 3fa3 4db4 5eb5 6fb6 7dc7 8ec8 9fc9 x - matrix(rnorm(700*700),700) rownames(x) - paste(R,1:700) colnames(x) - paste(C,1:700) system.time(foo - cbind(expand.grid(rownames(x),colnames(x)),as.vector(x))) user system elapsed 1.082 0.107 1.193 Thanks, Ian [a] [b] [c] [d]147 [e]258 [f]369 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@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. -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 __ 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] matrix to vector
Not sure it is really matrix to vector but here are a few of attempts: abM-matrix(1:9, nrow=3) rownames(abM) - letters[1:3] colnames(abM) - letters[4:6] data.frame( cols=colnames(abM)[col(abM)[1:9]], rows= rownames(abM) [row(abM)[1:9]], vals=abM[1:9]) cols rows vals 1da1 2db2 3dc3 4ea4 5eb5 6ec6 7fa7 8fb8 9fc9 matrix( c(colnames(abM)[col(abM)[1:9]], rownames(abM)[row(abM) [1:9]], abM[1:9]) ,ncol=3) # all text values since matrices in R need to be of homogenous type [,1] [,2] [,3] [1,] d a 1 [2,] d b 2 [3,] d c 3 [4,] e a 4 [5,] e b 5 [6,] e c 6 [7,] f a 7 [8,] f b 8 [9,] f c 9 But the most compact would be: library(reshape) melt(abM) X1 X2 value 1 a d 1 2 b d 2 3 c d 3 4 a e 4 5 b e 5 6 c e 6 7 a f 7 8 b f 8 9 c f 9 On May 29, 2009, at 2:43 PM, 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]147 [e]258 [f]369 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 -- David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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.