rdataframerep

Replicating a dataframe as a whole n times


I am trying to replicate a dataframe (zoo object) 50 times as a whole, and get the result as a matrix, but all the commands I have tried seems to be unsuccessful. I could easily write a function that would do this, but I was hoping the result could be easily achieved using rep.

Consider the following as an example

 x <- zoo(data.frame(A = c(1,2,3,4,5,6), B = c(7,8,9,10,11,12), C = c(13,14,15,16,17,18)), order.by = seq(as.Date("2012-01-01"), as.Date("2012-06-01"), by  = "month"))

 #> x
 #           A  B  C
 #2012-01-01 1  7 13
 #2012-02-01 2  8 14
 #2012-03-01 3  9 15
 #2012-04-01 4 10 16
 #2012-05-01 5 11 17
 #2012-06-01 6 12 18

Let's just try to replicate x 2 times. The end result I am looking for is:

 #      [,1] [,2] [,3]
 # [1,]    1    7   13
 # [2,]    2    8   14
 # [3,]    3    9   15
 # [4,]    4   10   16
 # [5,]    5   11   17
 # [6,]    6   12   18
 # [7,]    1    7   13
 # [8,]    2    8   14
 # [9,]    3    9   15
 #[10,]    4   10   16
 #[11,]    5   11   17
 #[12,]    6   12   18

This is what I have tried so far, but none of these work:

 matrix(rep(x,2), ncol = 3, byrow = T)

OR

 matrix(rep(x,2), ncol = 3, byrow = F)

OR

 matrix(rep(x, each = 2), ncol = 3)

How can I achieve this?


Solution

  • coredata(x)[rep(seq(nrow(x)),50),]
           A  B  C
      [1,] 1  7 13
      [2,] 2  8 14
      [3,] 3  9 15
      [4,] 4 10 16
      [5,] 5 11 17
      [6,] 6 12 18
    ...snip...
    [295,] 1  7 13
    [296,] 2  8 14
    [297,] 3  9 15
    [298,] 4 10 16
    [299,] 5 11 17
    [300,] 6 12 18