df <- data.frame(var1=c('a', 'b', 'c'), var2=c('d', 'e', 'f'), freq=1:3)
What is the simplest way to expand the first two columns of the data.frame above, so that each row appears 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
Here's one solution:
Result:
In case you have to do this operation on very large data.frames I would recommend converting it into a data.table and use the following, which should run much faster:
See how much faster this solution is:
Use
expandRows()
from thesplitstackshape
package:Simple syntax, very fast, works on
data.frame
ordata.table
.Result:
old question, new verb in tidyverse:
@neilfws's solution works great for
data.frame
s, but not fordata.table
s since they lack therow.names
property. This approach works for both:The code for
data.table
is a tad cleaner: