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]]
>
> ______________________________________________
> [email protected] 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:[EMAIL PROTECTED]                  UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

______________________________________________
[email protected] 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.

Reply via email to