Re: [R] How to transform the Matrix into the way I want it ???

2009-11-09 Thread William Dunlap

 -Original Message-
 From: r-help-boun...@r-project.org 
 [mailto:r-help-boun...@r-project.org] On Behalf Of Hongwei Dong
 Sent: Monday, November 09, 2009 2:24 PM
 To: R-help Forum
 Subject: [R] How to transform the Matrix into the way I want it ???
 
 Hi, R users,
 
 I'm trying to transform a matrix A into B (see below). Anyone 
 knows how to
 do it in R? Thanks.
 
 Matrix A (zone to zone travel time)
 
  zone z1 z2 z3  z1 0 2.9 4.3  z2 2.9 0 2.5  z3 4.3 2.5 0
 
 B:
 
 from to time z1 z1 0 z1 z2 2.9 z1 z3 4.3 z2 z1 2.9 z2 z2 0 z2 
 z3 2.5 z3 z1
 4.3 z3 z2 2.5 z3 z3 0

Try
   data.frame(From=rownames(A)[row(A)], To=colnames(A)[col(A)],
Time=as.vector(A))
You probably do not want a matrix because all of its elements
must have the same type, forcing Time to be character instead
of numeric.

E.g.,

 A-rbind(Anacortes=c(Anacortes=0,Seattle=90,Mount Vernon=20),
+  Seattle=c(Anacortes=80, Seattle=0, Mount Vernon=60),
+  Mount Vernon=c(Anacortes=20,Seattle=70,Mount Vernon=0))
 data.frame(From=rownames(A)[row(A)], To=colnames(A)[col(A)],
Time=as.vector(A))
  From   To Time
1AnacortesAnacortes0
2  SeattleAnacortes   80
3 Mount VernonAnacortes   20
4Anacortes  Seattle   90
5  Seattle  Seattle0
6 Mount Vernon  Seattle   70
7Anacortes Mount Vernon   20
8  Seattle Mount Vernon   60
9 Mount Vernon Mount Vernon0

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

 
 The real matrix I have is much larger, with more than 2000 
 zones. But I
 think it should be the same thing if I can transform A into B.
 
 Thanks.
 
 Garry
 
   [[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-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] How to transform the Matrix into the way I want it ???

2009-11-09 Thread Nikhil Kaza
 This is not an answer to your question, but I have used SparseM  
package to represent large travel time matrices efficiently.


?as.matrix.ssr

if the traveltime matrix is symmetric.

On 9 Nov 2009, at 5:24PM, Hongwei Dong wrote:


Hi, R users,

I'm trying to transform a matrix A into B (see below). Anyone knows  
how to

do it in R? Thanks.

Matrix A (zone to zone travel time)

zone z1 z2 z3  z1 0 2.9 4.3  z2 2.9 0 2.5  z3 4.3 2.5 0

B:

from to time z1 z1 0 z1 z2 2.9 z1 z3 4.3 z2 z1 2.9 z2 z2 0 z2 z3 2.5  
z3 z1

4.3 z3 z2 2.5 z3 z3 0

The real matrix I have is much larger, with more than 2000 zones.  
But I

think it should be the same thing if I can transform A into B.

Thanks.

Garry

[[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-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] How to transform the Matrix into the way I want it ???

2009-11-09 Thread David Winsemius


On Nov 9, 2009, at 5:24 PM, Hongwei Dong wrote:


Hi, R users,

I'm trying to transform a matrix A into B (see below). Anyone knows  
how to

do it in R? Thanks.

Matrix A (zone to zone travel time)

zone z1 z2 z3  z1 0 2.9 4.3  z2 2.9 0 2.5  z3 4.3 2.5 0

 ztz - read.table(textConnection( z1 z2 z3 \n z1 0 2.9 4.3 \n z2  
2.9 0 2.5 \n z3 4.3 2.5 0), header=T)

 ztz
z1  z2  z3
z1 0.0 2.9 4.3
z2 2.9 0.0 2.5
z3 4.3 2.5 0.0


B:

from to time z1 z1 0 z1 z2 2.9 z1 z3 4.3 z2 z1 2.9 z2 z2 0 z2 z3 2.5  
z3 z1

4.3 z3 z2 2.5 z3 z3 0


Now taking some liberties with coercion:

 as.data.frame.table(ztz, responseName=Time)
  Var1 Var2 Time
1   z1   z1  0.0
2   z2   z1  2.9
3   z3   z1  4.3
4   z1   z2  2.9
5   z2   z2  0.0
6   z3   z2  2.5
7   z1   z3  4.3
8   z2   z3  2.5
9   z3   z3  0.0


Or perhaps:

 data.frame(stack(as.data.frame(ztz)), From=colnames(ztz))
  values ind From
10.0  z1   z1
22.9  z1   z2
34.3  z1   z3
42.9  z2   z1
50.0  z2   z2
62.5  z2   z3
74.3  z3   z1
82.5  z3   z2
90.0  z3   z3



The real matrix I have is much larger, with more than 2000 zones.  
But I

think it should be the same thing if I can transform A into B.

Thanks.

Garry

[[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.


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.