Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replicating a dataframe as a whole n times

Tags:

dataframe

r

rep

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)

Could anyone help please?

Thank you,

like image 367
Mayou Avatar asked Aug 28 '13 13:08

Mayou


People also ask

How do you replicate data frames?

Method 1 : Using replicate() method The rbind() method is taken as the first argument of this method to combine data frames together. The second argument is the replicate() method which is used to create multiple copies of the rows of the data frames equivalent to the number of times same as replication factor.

How do you repeat a Dataframe in R?

In R, the easiest way to repeat rows is with the REP() function. This function selects one or more observations from a data frame and creates one or more copies of them. Alternatively, you can use the SLICE() function from the dplyr package to repeat rows.

How do you duplicate rows and times?

In the Copy and insert rows & columns dialog box, select Copy and insert rows option in the Type section, then select the data range you want to duplicate, and then specify the repeat time to duplicate the rows, see screenshot: 4.

How do you repeat a row multiple times in python?

In Python, if you want to repeat the elements multiple times in the NumPy array then you can use the numpy. repeat() function. In Python, this method is available in the NumPy module and this function is used to return the numpy array of the repeated items along with axis such as 0 and 1.


3 Answers

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
like image 182
James Avatar answered Sep 25 '22 21:09

James


sapply(x, rep.int, times=3)
#      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
# [7,] 1  7 13
# [8,] 2  8 14
# [9,] 3  9 15
# [10,] 4 10 16
# [11,] 5 11 17
# [12,] 6 12 18
# [13,] 1  7 13
# [14,] 2  8 14
# [15,] 3  9 15
# [16,] 4 10 16
# [17,] 5 11 17
# [18,] 6 12 18
like image 21
Roland Avatar answered Sep 25 '22 21:09

Roland


What about replicate?

do.call(rbind, replicate(5, as.matrix(x), simplify=FALSE))

Actually, much faster (but still not as fast as the accepted answer) would be to make use of coredata(), which I had forgotten about.

do.call(rbind, replicate(5, coredata(x), simplify = FALSE))
like image 44
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 23 '22 21:09

A5C1D2H2I1M1N2O1R2T1