I have below mentioned data frame:
Date Val1 Val2
2018-04-01 125 0.05
2018-04-03 458 2.99
2018-04-05 354 1.25
I want to add only missing dates considering Sys.Date()
(Here for example Sys.Date()
is 2018-04-06) in dataframe with corresponding val1 and val2 as 0.
I have tried: t2<-merge(data.frame(Date= seq(min(ymd(t1$Date)), max(ymd(date)), by = "days")), t1, by = "Date", all = TRUE)
Required Dataframe:
Date Val1 Val2
2018-04-01 125 0.05
2018-04-02 0 0
2018-04-03 458 2.99
2018-04-04 0 0
2018-04-05 354 1.25
2018-04-06 0 0
Here's a correction of your approach, in base R.
Replace
max(t1$Date)
bySys.Date()
in your real application:data
This could be done with
complete
If we need to pass multiple variables for the
fill
, create the list of columns that we need tofill
and then pass that as argument for the
fill
You could use
padr
.padr
is made for filling in missing date values. First you add the missing dates based on the interval, and if you do not want NA's you fill them with a value (or function of most occuring value)edit: added end_val to include the run until sys.Date()