Domanda

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,

È stato utile?

Soluzione

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

Altri suggerimenti

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

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))

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)

What about mapply() function?

mapply(rep, x, 3)

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top