[R] Matrix column flip when recycled

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sun Jul 14 17:49:19 CEST 2013

Only vectors recycle. If you don't want the behaviour if the matrix to reflect that of the underlying vector then don't use recycling. Instead use indexing.

M.1 <- M.2[rep(1:5,3),]
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
Sent from my phone. Please excuse my brevity.

Thiem  Alrik <thiem at sipo.gess.ethz.ch> wrote:

>Dear list,
>I have a matrix M.1 (30x2) into which I would like to paste another
>matrix M.2 (10x2) three times. However, the columns get flipped in
>every odd-numbered recycle run. How can I avoid this behavior?
>M.1 <- matrix(numeric(30*2), ncol = 2)
>M.2 <- t(combn(1:5, 2))
>M.1[, 1:2] <- M.2
>Many thanks for help,
>R-help at r-project.org mailing list
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list