0

I want to create a data frame by repeating rows by using content of a column in a data frame. Below is the source data frame.

data.frame(c("a","b","c"), c(4,5,6), c(2,2,3)) -> df
colnames(df) <- c("sample", "measurement", "repeat")
df
  sample measurement repeat
1      a           4      2
2      b           5      2
3      c           6      3

I want to repeat the rows by using the "repeat" column and its content to get a data frame like the one below. Ideally, I would like to have a function to this.

  sample measurement repeat
1      a           4      2
2      a           4      2
3      b           5      2
4      b           5      2
5      c           6      3
6      c           6      3
7      c           6      3

Thanks in advance!

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
KO 88
  • 55
  • 5

1 Answers1

0

Solved. df[rep(rownames(df), df$repeat), ] did the job.

KO 88
  • 55
  • 5