[R] rearrange data columns

2007-10-11 Thread Martin Ivanov
Dear R users, 
 I need to to the the following. Let a= 1 2 3 
 4 5 6 
 and b= -1 -2 -3  be (2x3) matrices.
-4 -5 -6 
 I need to combine the two matrices into a new (2x6) matrix like this: 
 
 ab = ( 1 -1 2 -2 3 -3 ) 
4 -4 5 -5 6 -6 
 
 How can this be done in R? 
 


-
Крайна цел - Да оцелееш! www.survivor.btv.bg

__
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] rearrange data columns

2007-10-11 Thread Robin Hankin

On 11 Oct 2007, at 12:55, Martin Ivanov wrote:

 Dear R users,
  I need to to the the following. Let a= 1 2 3
  4 5 6
  and b= -1 -2 -3  be (2x3) matrices.
 -4 -5 -6
  I need to combine the two matrices into a new (2x6) matrix like this:

  ab = ( 1 -1 2 -2 3 -3 )
 4 -4 5 -5 6 -6

  How can this be done in R?





  a
  [,1] [,2] [,3]
[1,]123
[2,]456
  b
  [,1] [,2] [,3]
[1,]   -1   -2   -3
[2,]   -4   -5   -6

  x - cbind(a,b)+NA
  x
  [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   NA   NA   NA   NA   NA   NA
[2,]   NA   NA   NA   NA   NA   NA
  x[,seq(from=1,by=2,len=3)] - a
  x[,seq(from=2,by=2,len=3)] - b
  x
  [,1] [,2] [,3] [,4] [,5] [,6]
[1,]1   -12   -23   -3
[2,]4   -45   -56   -6
 


HTH

rksh



 -
 Крайна цел - Да оцелееш! www.survivor.btv.bg

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

--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

__
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] rearrange data columns

2007-10-11 Thread Peter Dalgaard
Martin Ivanov wrote:
 Dear R users, 
  I need to to the the following. Let a= 1 2 3 
  4 5 6 
  and b= -1 -2 -3  be (2x3) matrices.
 -4 -5 -6 
  I need to combine the two matrices into a new (2x6) matrix like this: 
  
  ab = ( 1 -1 2 -2 3 -3 ) 
 4 -4 5 -5 6 -6 
  
  How can this be done in R? 
  
   
Here's one way:

 a - matrix(1:6, 2, byrow=T)
 b - -a
 ab - rbind(a,b); dim(ab)=c(2,6)
 ab
 [,1] [,2] [,3] [,4] [,5] [,6]
[1,]1   -12   -23   -3
[2,]4   -45   -56   -6

Here's another:

 ab - matrix(,2,6)
 ab[,seq(1,,2,3)] - a
 ab[,seq(2,,2,3)] - b
 ab
 [,1] [,2] [,3] [,4] [,5] [,6]
[1,]1   -12   -23   -3
[2,]4   -45   -56   -6



-- 
   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
~~ - ([EMAIL PROTECTED])  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] rearrange data columns

2007-10-11 Thread Douglas Bates
On 10/11/07, Martin Ivanov [EMAIL PROTECTED] wrote:
 Dear R users,
  I need to to the the following. Let a= 1 2 3
  4 5 6
  and b= -1 -2 -3  be (2x3) matrices.
 -4 -5 -6
  I need to combine the two matrices into a new (2x6) matrix like this:

  ab = ( 1 -1 2 -2 3 -3 )
 4 -4 5 -5 6 -6

  How can this be done in R?

 (a - matrix(1:6, nr = 2))
 [,1] [,2] [,3]
[1,]135
[2,]246
 (b - -a)
 [,1] [,2] [,3]
[1,]   -1   -3   -5
[2,]   -2   -4   -6
 (ans - rbind(a, b))
 [,1] [,2] [,3]
[1,]135
[2,]246
[3,]   -1   -3   -5
[4,]   -2   -4   -6
 dim(ans) - c(2, 6)
 ans
 [,1] [,2] [,3] [,4] [,5] [,6]
[1,]1   -13   -35   -5
[2,]2   -24   -46   -6

__
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] rearrange data columns

2007-10-11 Thread Henrique Dallazuanna
Try this also:

 ab - do.call(cbind, list(a, b))[,c(1,4,2,5,3,6)]


On 11/10/2007, Martin Ivanov [EMAIL PROTECTED] wrote:

 Dear R users,
 I need to to the the following. Let a= 1 2 3
 4 5 6
 and b= -1 -2 -3  be (2x3) matrices.
-4 -5 -6
 I need to combine the two matrices into a new (2x6) matrix like this:

 ab = ( 1 -1 2 -2 3 -3 )
4 -4 5 -5 6 -6

 How can this be done in R?



 -
 §¬§â§Ñ§Û§ß§Ñ §è§Ö§Ý - §¥§Ñ §à§è§Ö§Ý§Ö§Ö§ê! www.survivor.btv.bg

 __
 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] rearrange data columns

2007-10-11 Thread Ted Harding
On 11-Oct-07 12:09:19, Robin Hankin wrote:
 
 On 11 Oct 2007, at 12:55, Martin Ivanov wrote:
 
 Dear R users,
  I need to to the the following. Let a= 1 2 3
 4 5 6
  and b= -1 -2 -3  be (2x3) matrices.
 -4 -5 -6
  I need to combine the two matrices into a new (2x6) matrix like this:

  ab = ( 1 -1 2 -2 3 -3 )
 4 -4 5 -5 6 -6

  How can this be done in R?
 
   a
   [,1] [,2] [,3]
 [1,]123
 [2,]456
   b
   [,1] [,2] [,3]
 [1,]   -1   -2   -3
 [2,]   -4   -5   -6
 
   x - cbind(a,b)+NA
   x
   [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]   NA   NA   NA   NA   NA   NA
 [2,]   NA   NA   NA   NA   NA   NA
   x[,seq(from=1,by=2,len=3)] - a
   x[,seq(from=2,by=2,len=3)] - b
   x
   [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]1   -12   -23   -3
 [2,]4   -45   -56   -6
  
 
 
 HTH
 
 rksh

What's wrong with

a
## [,1] [,2] [,3]
##[1,]123
##[2,]456

b
## [,1] [,2] [,3]
##[1,]   -1   -2   -3
##[2,]   -4   -5   -6


cbind(a,b)[,c(1,4,2,5,3,6)]
## [,1] [,2] [,3] [,4] [,5] [,6]
##[1,]1   -12   -23   -3
##[2,]4   -45   -56   -6

??

Of course, for a more general case, you'd need a method for
generating the appropriate version of c(1,4,2,5,3,6) -- e.g.
something like Robin's seq.

Best wishes,
Ted.


E-Mail: (Ted Harding) [EMAIL PROTECTED]
Fax-to-email: +44 (0)870 094 0861
Date: 11-Oct-07   Time: 13:46:06
-- XFMail --

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