R

Asked • 10/11/19

Replicate each row of data.frame and specify the number of replications for each row?

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 thateach 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 3To this: df.expanded var1 var2 1 a d 2 b e 3 b e 4 c f 5 c f 6 c f

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.