This solution dplyr
obtains the dates of the first cases confirmed by "uf"
.
First dates become class "Date"
and confirmed
in class "numeric"
.
library(dplyr)
df <- df %>%
mutate(date = as.Date(date, "%d/%m/%Y"),
confirmed = as.numeric(as.character(confirmed)))
Now a basis is obtained for the first occurrences of confirmed cases.
prim_conf <- df %>%
filter(confirmed > 0) %>%
group_by(uf) %>%
summarise(date = first(date))
prim_conf
## A tibble: 2 x 2
# uf date
# <chr> <date>
#1 RJ 2020-06-05
#2 SP 2020-06-03
If you also want how many confirmed cases there were at those dates, a Join with the original basis will fetch these values.
prim_conf %>%
ungroup() %>%
left_join(df, by = c("date", "uf"))
## A tibble: 2 x 3
# uf date confirmed
# <chr> <date> <dbl>
#1 RJ 2020-06-05 1
#2 SP 2020-06-03 1
Or, more simply, without laying down the foundation,
prim_conf %>% left_join(df, by = c("date", "uf"))
## A tibble: 2 x 3
# uf date confirmed
# <chr> <date> <dbl>
#1 RJ 2020-06-05 1
#2 SP 2020-06-03 1
"filter" means that you want to be alone with the first occurrences or remove them? And "certain variable" is which of the three? The question is very unclear and, because of this, already with two votes to close.
– Rui Barradas
It means I want to stick with the first few occurrences. Certain variable applies to "Confirmed", since it is not trivial to want the first occurrences to be UF’s. Think of it as confirmed cases of disease in each state over time, I want to find the dates of the first occurrences. Thank you
– Rodolfo Bramont Eiriz de Souza