How can I repeat a data.frame?

后端 未结 4 1495
Happy的楠姐
Happy的楠姐 2021-01-14 02:10

It\'s easy to repeat a data.frame once,

mt2 <- rbind(mtcars, mtcars)

But what\'s an R-like way to do this generally? If I want 10 copies

4条回答
  •  长情又很酷
    2021-01-14 02:44

    Here's a very simple method:

    mtcars[rep(1:nrow(mtcars),2),]
    

    or using better "grammar":

    mtcars[rep(seq_len(nrow(mtcars)),2),]
    

    As GSee notes below, one difference here is that rbind will replicate the row names exactly, while using indexing will force unique row names by appending digits. Off the top of my head, I think the only fix would be to set the row names (again using rep) after the fact.

提交回复
热议问题