[R] rearrange data columns
Peter Dalgaard
P.Dalgaard at biostat.ku.dk
Thu Oct 11 14:11:24 CEST 2007
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 -1 2 -2 3 -3
[2,] 4 -4 5 -5 6 -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 -1 2 -2 3 -3
[2,] 4 -4 5 -5 6 -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
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list