df <- data.frame(var1 = c('a', 'b', 'c'), var2 = c('d', 'e', 'f'),
freq = 1:3)
What is the simplest way to expand each row the first two columns of the data.frame above, so that each row is repeated the number of times specified in the column 'freq'?
In other words, go from this:
df
var1 var2 freq
1 a d 1
2 b e 2
3 c f 3
To this:
df.expanded
var1 var2
1 a d
2 b e
3 b e
4 c f
5 c f
6 c f
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…