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]) > tmp a 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:[EMAIL PROTECTED] http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of philozine Sent: Friday, 15 June 2007 9:04 AM To: r-help@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@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. ______________________________________________ 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.