1

My data frame looks like this:

SIT LOC NB VAL
A a 1 0.256
A b 451 0.023
B a 2 0.123
B b 1 0.741

I would like to replicate NB times the rows. For example, I want the second line to be replicated 451 times. Then, I will not need anymore the column "NB" and this simplify the use of functions such as aggregate, etc.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2165907
  • 1,401
  • 3
  • 13
  • 28

1 Answers1

1

Use subsetting with rep:

DF <- read.table(text="SIT LOC NB VAL
A a 1 0.256
A b 451 0.023
B a 2 0.123
B b 1 0.741", header=TRUE)

DFrep <- DF[rep(seq_len(nrow(DF)), DF$NB),]
Roland
  • 127,288
  • 10
  • 191
  • 288