I have a data frame (dfA) showing for each country when a specific status occurred for the first time.
What I would like to get is a data frame (dfGoal) which shows for every day from January 1, 2020 until today which status was in place at this specific day. In addition to that I also would like to see since when the status is in place.
Here you can see a minimal example of my data and how my data frame should look like at the end.
pacman::p_load(lubridate)
# data frame which shows which status occurred when for the first time
dfA <- data.frame(country = c("Poland", "Poland", "Poland",
"Spain", "Spain",
"Italy", "Italy", "Italy", "Italy"),
status = c(0, 1, 2,
0, 2,
0, 1, 2, 0),
since = c(20200101, 20200228, 20200312,
20200118, 20200301,
20200101, 20200212, 20200304, 20200401))
date_seq <- seq(ymd('2020-01-01'),ymd('2020-04-18'), by = '1 day')
# target data frame
dfGoal <- data.frame(curr_date = c(date_seq, date_seq, date_seq),
country = c(replicate(109, "Poland"),
replicate(109, "Spain"),
replicate(109, "Italy")),
status = c(replicate(58, 0),
replicate(13, 1),
replicate(38, 2),
replicate(17, NA),
replicate(43, 0),
replicate(49, 2),
replicate(42, 0),
replicate(21, 1),
replicate(28, 2),
replicate(18, 0)),
since = c(replicate(58, 20200101),
replicate(13, 20200228),
replicate(38, 20200312),
replicate(17, NA),
replicate(43, 20200118),
replicate(49, 20200301),
replicate(42, 20200101),
replicate(21, 20200212),
replicate(28, 20200304),
replicate(18, 20200401)))
I tried to apply the suggestions made here, however I was not able to solve it.
Does anyone know how to do this with dplyr?