I've been seeing a lot of news outlets talking about a "country X reporting its lowest number of new coronavirus cases since date Y", so I wanted to try to do this in R, but I just can't figure out how.
Here's the data I have for Italy, for example:
italy <- tibble::tribble(
~country, ~date, ~cases_day,
"Italy", "2020-03-16", 3233L,
"Italy", "2020-03-17", 3526L,
"Italy", "2020-03-18", 4207L,
"Italy", "2020-03-19", 5322L,
"Italy", "2020-03-20", 5986L,
"Italy", "2020-03-21", 6557L,
"Italy", "2020-03-22", 5560L,
"Italy", "2020-03-23", 4789L,
"Italy", "2020-03-24", 5249L,
"Italy", "2020-03-25", 5210L,
"Italy", "2020-03-26", 6203L,
"Italy", "2020-03-27", 5909L,
"Italy", "2020-03-28", 5974L,
"Italy", "2020-03-29", 5217L,
"Italy", "2020-03-30", 4050L,
"Italy", "2020-03-31", 4053L,
"Italy", "2020-04-01", 4782L,
"Italy", "2020-04-02", 4668L,
"Italy", "2020-04-03", 4585L,
"Italy", "2020-04-04", 4805L,
"Italy", "2020-04-05", 4316L,
"Italy", "2020-04-06", 3599L,
"Italy", "2020-04-07", 3039L,
"Italy", "2020-04-08", 3836L,
"Italy", "2020-04-09", 4204L,
"Italy", "2020-04-10", 3951L,
"Italy", "2020-04-11", 4694L,
"Italy", "2020-04-12", 4092L,
"Italy", "2020-04-13", 3153L,
"Italy", "2020-04-14", 2972L
)
I want to create a column that tells me when was the last time the number of cases was below the one in the current line. So the desired result for the first 10 rows would be something like:
tibble::tribble(
~country, ~date, ~cases_day, ~minimum_since,
"Italy", "2020-03-16", 3233L, NA,
"Italy", "2020-03-17", 3526L, "2020-03-16",
"Italy", "2020-03-18", 4207L, "2020-03-17",
"Italy", "2020-03-19", 5322L, "2020-03-18",
"Italy", "2020-03-20", 5986L, "2020-03-19",
"Italy", "2020-03-21", 6557L, "2020-03-20",
"Italy", "2020-03-22", 5560L, "2020-03-19",
"Italy", "2020-03-23", 4789L, "2020-03-18",
"Italy", "2020-03-24", 5249L, "2020-03-23",
"Italy", "2020-03-25", 5210L, "2020-03-23"
)
I guess this could be done using something like accumulate? But I'm just stuck here. Thanks in advance for any help!