[R] How to duplicate each row in a data.frame?

Peng Yu pengyu.ut at gmail.com
Fri Dec 4 21:11:05 CET 2009


I want to duplicate each line in 'df' 3 times. But I'm confused why
'z' is a 6 by 4 matrix. Could somebody let me know what the correct
way is to duplicate each row of a data.frame?

df=expand.grid(x1=c('a','b'),x2=c('u','v'))
n=3
z=apply(df,1
    ,function(x){
      result=do.call(rbind,rep(list(x),n))
      result
    }
    )
z




More information about the R-help mailing list