[R] rearrange data columns

Douglas Bates bates at stat.wisc.edu
Thu Oct 11 14:27:28 CEST 2007


On 10/11/07, Martin Ivanov <tramni at abv.bg> 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,]    1    3    5
[2,]    2    4    6
> (b <- -a)
     [,1] [,2] [,3]
[1,]   -1   -3   -5
[2,]   -2   -4   -6
> (ans <- rbind(a, b))
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
[3,]   -1   -3   -5
[4,]   -2   -4   -6
> dim(ans) <- c(2, 6)
> ans
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1   -1    3   -3    5   -5
[2,]    2   -2    4   -4    6   -6



More information about the R-help mailing list