2

I have data.frame as follows :

duration    classlabel
 100           W
 120           1
 390           2
  30           3
  30           2
 150           3
  30           4
  60           3
  60           4
  30           3
 120           4
  30           3
 120           4

I have to make a number of lines according to duration with the class label in R. as an example, I have to make 100 rows with the class label 'W', and then 120 rows with the class label '2', etc. anyone, can help me to solve this problem?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Mera KD
  • 21
  • 1

1 Answers1

1

An option would be uncount

library(tidyr)
uncount(df1, duration, .remove = FALSE)

Or with rep from base R to replicate the sequence of rows by 'duration' column and expand the rows based on the numeric index

df1[rep(seq_len(nrow(df1)), df1$duration),]
akrun
  • 874,273
  • 37
  • 540
  • 662