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
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
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
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
Try this also: ab <- do.call("cbind", list(a, b))[,c(1,4,2,5,3,6)] On 11/10/2007, Martin Ivanov <tramni@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? > > > > ----------------------------------------------------------------- > §¬§â§Ñ§Û§ß§Ñ §è§Ö§Ý - §¥§Ñ §à§è§Ö§Ý§Ö§Ö§ê! 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]]
Try this: matrix(rbind(a, b), nrow(a)) 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? > > > > ----------------------------------------------------------------- > ?????? ??? - ?? ???????! 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. >