Hi R-listers, I'm using R only for a few basic functions but am having difficulty doing something that *should* be simple. I have an nxn matrix, Q, where Q[i,j] is a directed value (in this case, oil exports from i to j). Note that Q[i,j]~=Q[j,i]. I imported column names along with the matrix then copied them to the rows using rownames(Q) <- colnames(Q). Simple so far. What I'd like to do now is convert Q for export into a vector of values with the original row and column names intact. Having one vector each for row, column, and cell would be ideal, e.g., [1,1] = i's name, [1,2] = j's name, and [1,3] = Q[i, j]. But just being able to export my matrix data in vector form with the correct row/col names for each observation would be sufficient. Thus far I've tried c(), vector(), and a few others, but can't get the correct results. They do generate the correct vector of matrix values, but they do not appear to retain both row and column names. (Or, rather, I have not discovered how to make them do so.) To illustrate, my data currently look something like this: A B C D A | 0 |.1 |.4 |.6 | B |.2 | 0 |.2 |.1 | C |.5 |.9 | 0 |.9 | D |.7 | 0 |.3 | 0 | I would like them to look like this (at least when exported as a .txt file, if not necessary when displayed within R): i j Q | A | A | 0 | | A | B |.1 | | A | C |.4 | | A | D |.6 | | B | A |.2 | | B | B | 0 | | B | C |.2 | [...] and so on If anybody knows how to do this, I will be extremely appreciative! Best regards, --------------------------------- [[alternative HTML version deleted]]
Bill.Venables at csiro.au
2007-Jun-14 23:25 UTC
[R] Retain names in conversion of matrix to vector
There is a slightly surprising way to do this in one step. Here's an example> tmp <- matrix(1:16, 4, 4) > dimnames(tmp) <- list(letters[1:4], letters[1:4]) > tmpa b c d a 1 5 9 13 b 2 6 10 14 c 3 7 11 15 d 4 8 12 16> as.data.frame(as.table(tmp))Var1 Var2 Freq 1 a a 1 2 b a 2 3 c a 3 4 d a 4 5 a b 5 6 b b 6 7 c b 7 8 d b 8 9 a c 9 10 b c 10 11 c c 11 12 d c 12 13 a d 13 14 b d 14 15 c d 15 16 d d 16>Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: (I don't have one!) Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of philozine Sent: Friday, 15 June 2007 9:04 AM To: r-help at stat.math.ethz.ch Subject: [R] Retain names in conversion of matrix to vector Hi R-listers, I'm using R only for a few basic functions but am having difficulty doing something that *should* be simple. I have an nxn matrix, Q, where Q[i,j] is a directed value (in this case, oil exports from i to j). Note that Q[i,j]~=Q[j,i]. I imported column names along with the matrix then copied them to the rows using rownames(Q) <- colnames(Q). Simple so far. What I'd like to do now is convert Q for export into a vector of values with the original row and column names intact. Having one vector each for row, column, and cell would be ideal, e.g., [1,1] = i's name, [1,2] = j's name, and [1,3] = Q[i, j]. But just being able to export my matrix data in vector form with the correct row/col names for each observation would be sufficient. Thus far I've tried c(), vector(), and a few others, but can't get the correct results. They do generate the correct vector of matrix values, but they do not appear to retain both row and column names. (Or, rather, I have not discovered how to make them do so.) To illustrate, my data currently look something like this: A B C D A | 0 |.1 |.4 |.6 | B |.2 | 0 |.2 |.1 | C |.5 |.9 | 0 |.9 | D |.7 | 0 |.3 | 0 | I would like them to look like this (at least when exported as a .txt file, if not necessary when displayed within R): i j Q | A | A | 0 | | A | B |.1 | | A | C |.4 | | A | D |.6 | | B | A |.2 | | B | B | 0 | | B | C |.2 | [...] and so on If anybody knows how to do this, I will be extremely appreciative! Best regards, --------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch 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.
I think your.df <- as.data.frame( as.table( your.matrix ) ) colnames( your.df ) <- c( "i", "j", "Q" ) write.table( your.df, file = your.file.name, row.names=FALSE ) will do this. Chuck On Thu, 14 Jun 2007, philozine wrote:> Hi R-listers, > > I'm using R only for a few basic functions but am having difficulty doing something that *should* be simple. I have an nxn matrix, Q, where Q[i,j] is a directed value (in this case, oil exports from i to j). Note that Q[i,j]~=Q[j,i]. I imported column names along with the matrix then copied them to the rows using rownames(Q) <- colnames(Q). Simple so far. > > What I'd like to do now is convert Q for export into a vector of values with the original row and column names intact. Having one vector each for row, column, and cell would be ideal, e.g., [1,1] = i's name, [1,2] = j's name, and [1,3] = Q[i, j]. But just being able to export my matrix data in vector form with the correct row/col names for each observation would be sufficient. > > Thus far I've tried c(), vector(), and a few others, but can't get the correct results. They do generate the correct vector of matrix values, but they do not appear to retain both row and column names. (Or, rather, I have not discovered how to make them do so.) > > To illustrate, my data currently look something like this: > > A B C D > A | 0 |.1 |.4 |.6 | > B |.2 | 0 |.2 |.1 | > C |.5 |.9 | 0 |.9 | > D |.7 | 0 |.3 | 0 | > > I would like them to look like this (at least when exported as a .txt file, if not necessary when displayed within R): > > i j Q > | A | A | 0 | > | A | B |.1 | > | A | C |.4 | > | A | D |.6 | > | B | A |.2 | > | B | B | 0 | > | B | C |.2 | > [...] and so on > > If anybody knows how to do this, I will be extremely appreciative! > > Best regards, > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Fantastic. All of those methods worked, though I did have to first convert my matrices using the data.matrix command. Thank you for the assistance. Is there any equally simple way to re-convert the resulting table/matrix to its original NxN form? I do not see any obvious opposites to the cbind or as.table commands. Also, Jeff Laake, is there a way to have your routine output the vectors as numeric values (i.e., without the surrounding quote marks)? Best regards, Jeff Laake <Jeff.Laake@noaa.gov> wrote: > x=matrix(1:9,nrow=3) > colnames(x)=c("a","b","c") > row.names(x)=c("1","2","3") > x a b c 1 1 4 7 2 2 5 8 3 3 6 9 > cbind(as.vector(x),colnames(x)[as.vector(col(x))],row.names(x)[as.vector(row(x))]) [,1] [,2] [,3] [1,] "1" "a" "1" [2,] "2" "a" "2" [3,] "3" "a" "3" [4,] "4" "b" "1" [5,] "5" "b" "2" [6,] "6" "b" "3" [7,] "7" "c" "1" [8,] "8" "c" "2" [9,] "9" "c" "3" > philozine wrote:> Hi R-listers, > > I'm using R only for a few basic functions but am having difficulty doing something that *should* be simple. I have an nxn matrix, Q, where Q[i,j] is a directed value (in this case, oil exports from i to j). Note that Q[i,j]~=Q[j,i]. I imported column names along with the matrix then copied them to the rows using rownames(Q) <- colnames(Q). Simple so far. > > What I'd like to do now is convert Q for export into a vector of values with the original row and column names intact. Having one vector each for row, column, and cell would be ideal, e.g., [1,1] = i's name, [1,2] = j's name, and [1,3] = Q[i, j]. But just being able to export my matrix data in vector form with the correct row/col names for each observation would be sufficient. > > Thus far I've tried c(), vector(), and a few others, but can't get the correct results. They do generate the correct vector of matrix values, but they do not appear to retain both row and column names. (Or, rather, I have not discovered how to make them do so.) > > To illustrate, my data currently look something like this: > > A B C D > A | 0 |.1 |.4 |.6 | > B |.2 | 0 |.2 |.1 | > C |.5 |.9 | 0 |.9 | > D |.7 | 0 |.3 | 0 | > > I would like them to look like this (at least when exported as a .txt file, if not necessary when displayed within R): > > i j Q > | A | A | 0 | > | A | B |.1 | > | A | C |.4 | > | A | D |.6 | > | B | A |.2 | > | B | B | 0 | > | B | C |.2 | > [...] and so on > > If anybody knows how to do this, I will be extremely appreciative! > > Best regards, > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >--------------------------------- The fish are biting. [[alternative HTML version deleted]]