[R] rbind question

Emmanuel Paradis paradis at isem.univ-montp2.fr
Tue Jul 8 16:13:03 CEST 2003


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 else

This 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




More information about the R-help mailing list