How can I add rows to an R data frame every other row?

前端 未结 5 1538
情书的邮戳
情书的邮戳 2020-12-19 07:49

Brief: how can I add m rows to my m X n data frame, where each new row is inserted after each existing row? I will essentially copy the existing ro

5条回答
  •  一整个雨季
    2020-12-19 08:33

    modified from "e4e5f4's" response

    Insert blank rows betweens rows

        # sample matrix of df 
        old <-matrix(1:9, ncol=3)
    
        # double all rows 
        new <- old[rep(1:nrow(old),1,each=2),]
    
        # replace all duplicates with blank cells
        new[c(seq(2, dim(new)[1], by=2)), ] <- ""
    
        old # original 
        new # all ok ;)
    

提交回复
热议问题