2
I have 3 dataframes with 2 columns and 5 rows each, as follows:
Dataframe 1:
estado casosNovos
<chr> <dbl>
1 SP 3189
2 CE 1921
3 AM 1365
4 PA 1135
5 MA 938
I would like to order the DF2
and DF3
in the same order as the column "status" of DF1
. I tried to use the functions sort
and order
, but states end up in different positions. For example, when using the function order
:
DF2 <- DF2[order(DF1$estado),]
Upshot:
estado casosNovos
<chr> <dbl>
1 MA 689
2 CE 744
3 SP 3378
4 PA 1285
5 AM 1648
Code I am using:
library(tidyverse)
dados <- read_csv("https://brasil.io/dataset/covid19/caso_full/?place_type=state&is_repeated=False&format=csv") %>%
select(-c(epidemiological_week, order_for_place, city, city_ibge_code, place_type,
last_available_confirmed_per_100k_inhabitants, last_available_death_rate,
estimated_population_2019, is_last, is_repeated)) %>%
arrange(state, date)
names(dados) <- c("data", "estado", "casosAcumulados", "casosNovos",
"obitosAcumulados", "obitosNovos")
DF1 <- dados %>%
filter(data == max(data)) %>%
select(estado, casosNovos) %>%
arrange(desc(casosNovos)) %>%
head(5L)
DF2 <- dados %>%
filter(estado %in% DF1$estado, data == max(data) - 1) %>%
select(estado, casosNovos)
DF3 <- dados %>%
filter(estado %in% DF1$estado, data == max(data) - 7) %>%
select(estado, casosNovos)
DF2 <- DF2[order(DF1$estado),]
DF3 <- DF3[order(DF1$estado),]