I have a vector of dates like this:
ds <- lubridate::as_date(c("2015-11-23", "2015-11-24", "2015-11-25",
"2015-11-26", "2015-11-27", "2015-11-30", "2015-12-01", "2015-12-02",
"2015-12-03", "2015-12-04"))
This vector contains date in increasing order but in between some days are missing. In this example, Nov 28 and Nov 29 are missing, for example.
I now want to turn these dates into dummies.
One dummies should just be the month, the other dummy should indicate the position within each month. In the above example, the first observed value in Nov 2015 is Nov 23, 2015.
In this case the result would be:
df <- data.frame(November = c(1, 1, 1, 1, 1, 1, 0 ,0 ,0 ,0),
December = c(0, 0, 0, 0, 0, 0, 1 ,1 ,1 ,1),
d1 = c(1, 0,0,0,0,0,1,0,0,0),
d2 = c(0, 1,0,0,0,0,0,1,0,0),
d3 = c(0, 0,1,0,0,0,0,0,1,0),
d4 = c(0, 0,0,1,0,0,0,0,0,1),
d5 = c(0, 0,0,0,1,0,0,0,0,0),
d6 = c(0, 0,0,0,0,1,0,0,0,0))
> df
November December d1 d2 d3 d4 d5 d6
1 1 0 1 0 0 0 0 0
2 1 0 0 1 0 0 0 0
3 1 0 0 0 1 0 0 0
4 1 0 0 0 0 1 0 0
5 1 0 0 0 0 0 1 0
6 1 0 0 0 0 0 0 1
7 0 1 1 0 0 0 0 0
8 0 1 0 1 0 0 0 0
9 0 1 0 0 1 0 0 0
10 0 1 0 0 0 1 0 0
where the d1
mean first observed date in this specific month.
Please note that it should generalize to many years.
What I tried is this:
nov <- ds[months(ds) == 'November']
d1 <- ifelse(ds %in% nov & ds == dplyr::first(nov), 1, 0 )