I am sure this is super simple, but I just can't find it..
How would you duplicate the first 10, 20 or 50 rows for all variables in a dataset and just add them as new rows?
I am sure this is super simple, but I just can't find it..
How would you duplicate the first 10, 20 or 50 rows for all variables in a dataset and just add them as new rows?
R's subsetting allows you to assign objects to indexes that don't exist, so you can just set new rows to contain the original rows:
df <- data.frame(x = 1:10, y = 11:20)
df[11:20, ] <- df[1:10, ]
df
#> x y
#> 1 1 11
#> 2 2 12
#> 3 3 13
#> 4 4 14
#> 5 5 15
#> 6 6 16
#> 7 7 17
#> 8 8 18
#> 9 9 19
#> 10 10 20
#> 11 1 11
#> 12 2 12
#> 13 3 13
#> 14 4 14
#> 15 5 15
#> 16 6 16
#> 17 7 17
#> 18 8 18
#> 19 9 19
#> 20 10 20
Created on 2018-05-14 by the reprex package (v0.2.0).