How can you replicate each row of an R data.frame and specify the number of replications for each ro

Posted by wkmor1 on Stack Overflow See other posts from Stack Overflow or by wkmor1
Published on 2010-05-24T04:46:11Z Indexed on 2010/05/24 4:51 UTC
Read the original article Hit count: 216

Filed under:
|
|
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

© Stack Overflow or respective owner

Related posts about r

    Related posts about data.frame