Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
840 views
in Technique[技术] by (71.8m points)

insert - Rstudio filling missing rows

I have the following question : is it possible to fill some missing rows in a database ?

Please find below an exemple of what I attempt to do. I have a database with some sellers and buyers and monthly transactions

library(data.table)
set.seed(1)
Data <- data.frame(
  Month = c(1,5,3,4,5,3,4,5,6),
  Code_ID_Buy = c("100D","100D","102D","102D","102D","100D","100D","100D","100D"),
  Code_ID_Sell = c("98C","98C","99C","99C","99C","25A","25A","25A","25A"),
  Contract_Size = c(100,20,120,300,120,30,25,150,250)
)

View(Data)

The issue is that some months are missing, hence I would need to insert one row each time there is a missing row (an after the last occurence) with the ID of the buyer the seller and a contract value of 0, so that the data looks like :

Data <- data.frame(
  Month = c(1,2,3,4,5,6,3,4,5,6,3,4,5,6,7),
  Code_ID_Buy = c("100D","100D","100D","100D","100D","100D","102D","102D","102D","102D","100D","100D","100D","100D","100D"),
  Code_ID_Sell = c("98C","98C","98C","98C","98C","98C","99C","99C","99C","99C","25A","25A","25A","25A","25A"),
  Contract_Size = c(100,0,0,0,20,0,120,300,120,0,30,25,150,250,0)
)
View(Data)

Thank you in advance,

Kind regards,

question from:https://stackoverflow.com/questions/65939631/rstudio-filling-missing-rows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...