[R] rearrange data columns

Robin Hankin r.hankin at noc.soton.ac.uk
Thu Oct 11 14:09:19 CEST 2007


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,]    1    2    3
[2,]    4    5    6
 > 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   -1    2   -2    3   -3
[2,]    4   -4    5   -5    6   -6
 >


HTH

rksh


>
> -----------------------------------------------------------------
> Крайна цел - Да оцелееш! www.survivor.btv.bg
>
> ______________________________________________
> R-help at 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



More information about the R-help mailing list