I have a data table that looks like the following:
id firstd lastd treat
1 2003-03-23 2003-03-25 1
1 2003-03-24 2003-03-25 NA
1 2003-03-25 2003-03-25 NA
1 2003-05-13 2003-05-15 0
1 2003-05-14 2003-05-15 NA
1 2003-05-15 2003-05-15 NA
2 2004-04-28 2004-04-30 0
2 2004-04-29 2003-04-30 NA
2 2004-04-30 2003-04-30 NA
I want to carry through the values of the column treat through the date range from firstd to firstd==lastd by id so the NAs are filled in with the given values.
Ideally, it will look like the following:
id firstd lastd treat
1 2003-03-23 2003-03-25 1
1 2003-03-24 2003-03-25 1
1 2003-03-25 2003-03-25 1
1 2003-05-13 2003-05-15 0
1 2003-05-14 2003-05-15 0
1 2003-05-15 2003-05-15 0
2 2004-04-28 2004-04-30 0
2 2004-04-29 2003-04-30 0
2 2004-04-30 2003-04-30 0
I know how to carry a value through by one column, but have not done it with the added complexity of a given date range. Does anyone know how to do this?
The code I generally use when carrying forward values through given columns is the following-
one[, treat:= treat[!is.na(treat)][1], by = id]
Does anyone know how to amend this piece of code to also consider the given date ranges? Or would have any further suggestions?