Hi I am trying to replicate a vector in n rows for comparison purposes with another matrix. foo <- c(1,2,3) bar <- rbind(foo,foo) # does the trick for 2 rows bar <- rbind(rep(foo,2)) # does something else How do I generate a matrix with all rows=foo without writing 'foo' n times as arg? Thanks, David
What about:> matrix(rep(foo,2),ncol=length(foo),byrow=TRUE)-----Original Message----- From: David Andel [mailto:andel@ifi.unizh.ch] Sent: 08 July 2003 14:58 To: r-help@stat.math.ethz.ch Subject: [R] rbind question Hi I am trying to replicate a vector in n rows for comparison purposes with another matrix. foo <- c(1,2,3) bar <- rbind(foo,foo) # does the trick for 2 rows bar <- rbind(rep(foo,2)) # does something else How do I generate a matrix with all rows=foo without writing 'foo' n times as arg? Thanks, David ______________________________________________ R-help@stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help KSS Ltd Seventh Floor St James's Buildings 79 Oxford Street Manchester M1 6SS England Company Registration Number 2800886 Tel: +44 (0) 161 228 0040 Fax: +44 (0) 161 236 6305 mailto:kssg@kssg.com http://www.kssg.com The information in this Internet email is confidential and m...{{dropped}}
Using the recyling rule: bar <- matrix(foo, nrow=n, ncol=length(foo), byrow=TRUE) HTH, Andy> -----Original Message----- > From: David Andel [mailto:andel at ifi.unizh.ch] > Sent: Tuesday, July 08, 2003 9:58 AM > To: r-help at stat.math.ethz.ch > Subject: [R] rbind question > > > Hi > > I am trying to replicate a vector in n rows for comparison > purposes with > another matrix. > > foo <- c(1,2,3) > bar <- rbind(foo,foo) # does the trick for 2 rows > bar <- rbind(rep(foo,2)) # does something else > > How do I generate a matrix with all rows=foo without writing > 'foo' n times as > arg? > > Thanks, > David > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, ...{{dropped}}
How about foo <- c(1,2,3) bar <- matrix(rep(foo, 5), ncol = length(foo), byrow = TRUE)>Date: 8 Jul 2003 15:58:25 +0200 >From: "David Andel" <andel at ifi.unizh.ch> >To: r-help at stat.math.ethz.ch >MIME-Version: 1.0 >Content-Transfer-Encoding: 8bit >X-Virus-Scanned: by amavisd-milter (http://amavis.org/) >X-Virus-Scanned: by amavisd-milter (http://amavis.org/) >X-Spam-Status: No, hits=-5.3 required=5.0 tests=BAYES_01, HAS_ORGANIZATIONversion=2.54>X-Spam-Level: >X-Spam-Checker-Version: SpamAssassin 2.54 (1.174.2.17-2003-05-11-exp) >Subject: [R] rbind question >X-BeenThere: r-help at stat.math.ethz.ch >X-Mailman-Version: 2.1.2 >List-Id: Main R Mailing List: Primary help <r-help.stat.math.ethz.ch> >List-Help: <mailto:r-help-request at stat.math.ethz.ch?subject=help> >List-Post: <mailto:r-help at stat.math.ethz.ch> >List-Subscribe: <https://www.stat.math.ethz.ch/mailman/listinfo/r-help>,<mailto:r-help-request at stat.math.ethz.ch?subject=subscribe>>List-Archive: <https://www.stat.math.ethz.ch/pipermail/r-help> >List-Unsubscribe: <https://www.stat.math.ethz.ch/mailman/listinfo/r-help>,<mailto:r-help-request at stat.math.ethz.ch?subject=unsubscribe>> >Hi > >I am trying to replicate a vector in n rows for comparison purposes with >another matrix. > >foo <- c(1,2,3) >bar <- rbind(foo,foo) # does the trick for 2 rows >bar <- rbind(rep(foo,2)) # does something else > >How do I generate a matrix with all rows=foo without writing 'foo' n times as >arg? > >Thanks, >David > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-helpJianhua Zhang Department of Biostatistics Dana-Farber Cancer Institute 44 Binney Street Boston, MA 02115-6084
David Andel wrote:> Hi > > I am trying to replicate a vector in n rows for comparison purposes with > another matrix. > > foo <- c(1,2,3) > bar <- rbind(foo,foo) # does the trick for 2 rows > bar <- rbind(rep(foo,2)) # does something else > > How do I generate a matrix with all rows=foo without writing 'foo' n times as > arg? >How about: foo <- 1:3 n <- 10 # number of rows matrix(rep(foo, n), nr = n, byrow = TRUE) Regards, Sundar
Hi David, At 15:58 08/07/2003 +0200, vous avez ?crit:>Hi > >I am trying to replicate a vector in n rows for comparison purposes with >another matrix. > >foo <- c(1,2,3) >bar <- rbind(foo,foo) # does the trick for 2 rows >bar <- rbind(rep(foo,2)) # does something elseThis is because `rep()' is executed before `rbind()'.>How do I generate a matrix with all rows=foo without writing 'foo' n times as >arg?I guess there are several possible ways to do what you want. Here's my solution (already posted by Andy Liaw...): R> foo <- c(1,2,3) R> N <- 10 R> bar <- matrix(foo, nr = N, nc = length(foo), byrow = TRUE) R> bar [,1] [,2] [,3] [1,] 1 2 3 [2,] 1 2 3 [3,] 1 2 3 [4,] 1 2 3 [5,] 1 2 3 [6,] 1 2 3 [7,] 1 2 3 [8,] 1 2 3 [9,] 1 2 3 [10,] 1 2 3 Emmanuel Paradis
do.call("rbind", rep(list(foo), n)) Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") David Andel wrote:>Hi > >I am trying to replicate a vector in n rows for comparison purposes with >another matrix. > >foo <- c(1,2,3) >bar <- rbind(foo,foo) # does the trick for 2 rows >bar <- rbind(rep(foo,2)) # does something else > >How do I generate a matrix with all rows=foo without writing 'foo' n times as >arg? > >Thanks, >David > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > >
On 8 Jul 2003, David Andel wrote:> Hi > > I am trying to replicate a vector in n rows for comparison purposes with > another matrix. > > foo <- c(1,2,3) > bar <- rbind(foo,foo) # does the trick for 2 rows > bar <- rbind(rep(foo,2)) # does something else > > How do I generate a matrix with all rows=foo without writing 'foo' n times as > arg?Like this?> foo <- c(1,2,3) > n <- 5 > bar <- matrix(foo, nrow = n, ncol = length(foo), byrow = TRUE) > bar[,1] [,2] [,3] [1,] 1 2 3 [2,] 1 2 3 [3,] 1 2 3 [4,] 1 2 3 [5,] 1 2 3 G.> > Thanks, > David > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >-- --- G?ran Brostr?m tel: +46 90 786 5223 Department of Statistics fax: +46 90 786 6614 Ume? University http://www.stat.umu.se/egna/gb/ SE-90187 Ume?, Sweden e-mail: gb at stat.umu.se