0

I want to append n identical data frames to each other. This works if n=2:

> d = data.frame(a=1:2)
> dplyr::bind_rows(d,d, .id="id")
# id a
# 1  1
# 1  2
# 2  1
# 2  2

But I don't know how to extend this to larger values of n, without manually typing something like dplyr::bind_rows(d,d,d .id="id") for n = 3. Is there some smart way to programatically feed a list of d with length=n to the bind_rows command? This doesn't work: dplyr::bind_rows(rep(d,3), .id="id").

Also - is there a data.table solution?

Gautam
  • 2,597
  • 1
  • 28
  • 51
Stuart
  • 131
  • 1
  • 4
  • 1
    `bind_rows` accepts a list of data.frames. So just put all your `d`'s into a list and then pass that list to `bind_rows` – divibisan Aug 22 '18 at 18:56
  • Or `data.table::rbindlist()` especially if speed is an issue – Mako212 Aug 22 '18 at 18:56
  • 2
    You could replicate the same dataset with `rep(list(d), 3)`. – aosmith Aug 22 '18 at 18:57
  • 1
    I'd recommend the second answer at the dupe, `result = d[rep(1:nrow(d), n), ]`. It's easy to stick on an `id` column posthoc:: `result$id = rep(1:n, each = nrow(d))`. (Will work equally well with data tables.) But there are also answers at the dupe using `purrr` and other fancier, less efficient methods. – Gregor Thomas Aug 22 '18 at 19:18

1 Answers1

0

Here's a solution using data.table::rbindlist():

library(data.table)
l <- list(mtcars, mtcars*2, mtcars*3) 

DATA

# Check l
> str(l)
List of 3
 $ :'data.frame':   32 obs. of  11 variables:
  ..$ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
  ..$ cyl : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
  ..$ disp: num [1:32] 160 160 108 258 360 ...
  ..$ hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
  ..$ drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
  ..$ wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
  ..$ qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
  ..$ vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
  ..$ am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
  ..$ gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
  ..$ carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
 $ :'data.frame':   32 obs. of  11 variables:
  ..$ mpg : num [1:32] 42 42 45.6 42.8 37.4 36.2 28.6 48.8 45.6 38.4 ...
  ..$ cyl : num [1:32] 12 12 8 12 16 12 16 8 8 12 ...
  ..$ disp: num [1:32] 320 320 216 516 720 ...
  ..$ hp  : num [1:32] 220 220 186 220 350 210 490 124 190 246 ...
  ..$ drat: num [1:32] 7.8 7.8 7.7 6.16 6.3 5.52 6.42 7.38 7.84 7.84 ...
  ..$ wt  : num [1:32] 5.24 5.75 4.64 6.43 6.88 6.92 7.14 6.38 6.3 6.88 ...
  ..$ qsec: num [1:32] 32.9 34 37.2 38.9 34 ...
  ..$ vs  : num [1:32] 0 0 2 2 0 2 0 2 2 2 ...
  ..$ am  : num [1:32] 2 2 2 0 0 0 0 0 0 0 ...
  ..$ gear: num [1:32] 8 8 8 6 6 6 6 8 8 8 ...
  ..$ carb: num [1:32] 8 8 2 2 4 2 8 4 4 8 ...
 $ :'data.frame':   32 obs. of  11 variables:
  ..$ mpg : num [1:32] 63 63 68.4 64.2 56.1 54.3 42.9 73.2 68.4 57.6 ...
  ..$ cyl : num [1:32] 18 18 12 18 24 18 24 12 12 18 ...
  ..$ disp: num [1:32] 480 480 324 774 1080 ...
  ..$ hp  : num [1:32] 330 330 279 330 525 315 735 186 285 369 ...
  ..$ drat: num [1:32] 11.7 11.7 11.55 9.24 9.45 ...
  ..$ wt  : num [1:32] 7.86 8.62 6.96 9.64 10.32 ...
  ..$ qsec: num [1:32] 49.4 51.1 55.8 58.3 51.1 ...
  ..$ vs  : num [1:32] 0 0 3 3 0 3 0 3 3 3 ...
  ..$ am  : num [1:32] 3 3 3 0 0 0 0 0 0 0 ...
  ..$ gear: num [1:32] 12 12 12 9 9 9 9 12 12 12 ...
  ..$ carb: num [1:32] 12 12 3 3 6 3 12 6 6 12 ...

CODE & OUTPUT

dat <- rbindlist(l, use.names = T, fill = T)

# Verify if data looks like what we want 
> str(dat)
Classes ‘data.table’ and 'data.frame':  96 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
 - attr(*, ".internal.selfref")=<externalptr> 
Gautam
  • 2,597
  • 1
  • 28
  • 51