R code to insert rows based on a column's value and increment it by 1

后端 未结 3 797
梦谈多话
梦谈多话 2021-01-26 05:08

I have two columns in my data as shown below. I want to insert rows based on the value in Total column. Eg. For record1, the total value is 9. So, there should be 9 records for

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-26 05:58

    library(data.table)
    setDT(df)[,.(Total=1:Total), code]
    

    Or base R:

    with(df, data.frame(code=rep(code, Total), Total=sequence(Total)))
    

提交回复
热议问题