[R] How to duplicate each row in a data.frame?
David Freedman
3.14david at gmail.com
Fri Dec 4 21:21:36 CET 2009
I *think* this is from from 'StatsRUs' - how about
as.data.frame(lapply(df,function(x)rep(x,n)))
hth, david freedman
pengyu.ut wrote:
>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
--
View this message in context: http://n4.nabble.com/How-to-duplicate-each-row-in-a-data-frame-tp948830p948839.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list