Replicating a dataframe as a whole n times
Asked Answered
C

6

17

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?

Caudillo answered 28/8, 2013 at 13:2 Comment(1)
Have a look at @Joran's answer hereBane
M
16
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
Musset answered 28/8, 2013 at 13:12 Comment(0)
C
12
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
Cervelat answered 28/8, 2013 at 13:11 Comment(1)
You also should add as.data.frame.Sianna
S
7

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))
Stinkpot answered 28/8, 2013 at 13:10 Comment(0)
J
1

What about mapply() function?

mapply(rep, x, 3)
Joellyn answered 30/5, 2021 at 12:45 Comment(0)
C
1

purrr style

df <- iris[1:3,]
N <- 3
library(tidyverse)
reduce(seq_len(N -1), .init = df, ~bind_rows(.x, df))
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          5.1         3.5          1.4         0.2  setosa
#> 5          4.9         3.0          1.4         0.2  setosa
#> 6          4.7         3.2          1.3         0.2  setosa
#> 7          5.1         3.5          1.4         0.2  setosa
#> 8          4.9         3.0          1.4         0.2  setosa
#> 9          4.7         3.2          1.3         0.2  setosa

Created on 2021-05-30 by the reprex package (v2.0.0)


similar baseR style

df <- iris[1:3,]
N <- 3

Reduce(function(.x, .y) rbind(.x, df), seq_len(N -1), init = df)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          5.1         3.5          1.4         0.2  setosa
#> 5          4.9         3.0          1.4         0.2  setosa
#> 6          4.7         3.2          1.3         0.2  setosa
#> 7          5.1         3.5          1.4         0.2  setosa
#> 8          4.9         3.0          1.4         0.2  setosa
#> 9          4.7         3.2          1.3         0.2  setosa

Created on 2021-05-30 by the reprex package (v2.0.0)

Chur answered 30/5, 2021 at 12:53 Comment(0)
S
0

We could use bind_rows (combines all dataframes in a list) in dplyr package, so we can avoid do.call() together with rbind():

library(dplyr)
bind_rows(replicate(2, x, simplify = FALSE))

Output:

       A     B     C
   <dbl> <dbl> <dbl>
 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
Stent answered 30/5, 2021 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.