我的data.frame如下:
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
我必须根据持续时间用R中的类标签进行多行处理。例如,我必须用类标签“ W”进行100行,然后用类标签“ 2”进行120行等 任何人,可以帮助我解决这个问题吗?
答案 0 :(得分:1)
选项为uncount
library(tidyr)
uncount(df1, duration, .remove = FALSE)
或者使用rep
中的base R
复制“ duration”列中的行序列,并根据数字索引展开行
df1[rep(seq_len(nrow(df1)), df1$duration),]