0

I have a problem with restructuring data. For the entire data set, I need to duplicate each row and insert it to the row right after it, such that

row 1
duplicate of row 1
row 2
duplicate of row 2

and so on and so forth for the entire data set.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197

1 Answers1

3

You could try rep

df1[rep(1:nrow(df1),each=2),,drop=FALSE]

Or using splitstackshape

library(splitstackshape)
expandRows(df1, 2, count.is.col = FALSE)

data

df1 <- data.frame(Col1=1:5, Col2=6:10)
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    You might want to use `seq_len(nrow(df1))` instead of `1:nrow(df1)` in case `df1` has zero rows. – Samuel Isaacson Dec 30 '14 at 21:28
  • No need for an "index" column. Use the `count.is.col` argument. When set to `FALSE`, an arbitrary vector can be entered. When a single value is entered, it is replicated for all rows. – A5C1D2H2I1M1N2O1R2T1 Jan 02 '15 at 13:55