Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
839 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 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...