Preciso calcular a porcentagem de dias que estejam em determinado intervalo de valores na coluna analise (valores estão em porcentagem) em relação ao quantitativo total de dias referente aquele mês.
Inicialmente faço o levantamento de quantos dias cada mês possui, faço isso principalmente devido ao mês de fevereiro que pode variar:
qtd_dias_mes <- dados %>%
group_by(mes) %>%
select(analise) %>%
dplyr::summarise(n()) %>%
dplyr::rename("qtd_dias_mes" = "n()")
View(qtd_dias_mes)
Agora faço o levantamento de quantos dias estão entre o intervalo que seja maior que 75 e menor que 100:
analise_75_e_100 <- dados %>%
filter(dados$analise > 75 & dados$analise < 100) %>%
group_by(mes) %>%
dplyr::summarise(n())
os valores entregues pelo dplyr::summarise(n()) não contempla o mês 6 (com o dataframe completo), e ao fazer o cálculo que preciso (abaixo), acaba aparecendo o mês 6 (o que não deveria) e com valores errados a partir dele, sendo que deveria aparecer 0 para o mês 6:
porc_dias_entre_75_e_100 = (analise_75_e_100$`n()` / qtd_dias_mes$qtd_dias_mes) * 100
de forma a simplificar minha pergunta aqui, apresentei acima o cálculo com esse intervalo, contudo irei calcular para diferentes intervalos de porcentagem, podendo aparecer menos não contemplados, devendo então serem preenchidos com zero. Quais alternativas para esses cômputos ficarem corretos?
Meus dados possuem 10957 linhas (1/1/1990 a 31/12/2019), talvez fazendo com a amostra abaixo altere o mês 6 que exemplifiquei acima, contudo o raciocínio é o mesmo.
> dput(head(dados, 200))
structure(list(data = c("1/1/1990", "1/2/1990", "1/3/1990", "1/4/1990",
"1/5/1990", "1/6/1990", "1/7/1990", "1/8/1990", "1/9/1990", "1/10/1990",
"1/11/1990", "1/12/1990", "1/13/1990", "1/14/1990", "1/15/1990",
"1/16/1990", "1/17/1990", "1/18/1990", "1/19/1990", "1/20/1990",
"1/21/1990", "1/22/1990", "1/23/1990", "1/24/1990", "1/25/1990",
"1/26/1990", "1/27/1990", "1/28/1990", "1/29/1990", "1/30/1990",
"1/31/1990", "2/1/1990", "2/2/1990", "2/3/1990", "2/4/1990",
"2/5/1990", "2/6/1990", "2/7/1990", "2/8/1990", "2/9/1990", "2/10/1990",
"2/11/1990", "2/12/1990", "2/13/1990", "2/14/1990", "2/15/1990",
"2/16/1990", "2/17/1990", "2/18/1990", "2/19/1990", "2/20/1990",
"2/21/1990", "2/22/1990", "2/23/1990", "2/24/1990", "2/25/1990",
"2/26/1990", "2/27/1990", "2/28/1990", "3/1/1990", "3/2/1990",
"3/3/1990", "3/4/1990", "3/5/1990", "3/6/1990", "3/7/1990", "3/8/1990",
"3/9/1990", "3/10/1990", "3/11/1990", "3/12/1990", "3/13/1990",
"3/14/1990", "3/15/1990", "3/16/1990", "3/17/1990", "3/18/1990",
"3/19/1990", "3/20/1990", "3/21/1990", "3/22/1990", "3/23/1990",
"3/24/1990", "3/25/1990", "3/26/1990", "3/27/1990", "3/28/1990",
"3/29/1990", "3/30/1990", "3/31/1990", "4/1/1990", "4/2/1990",
"4/3/1990", "4/4/1990", "4/5/1990", "4/6/1990", "4/7/1990", "4/8/1990",
"4/9/1990", "4/10/1990", "4/11/1990", "4/12/1990", "4/13/1990",
"4/14/1990", "4/15/1990", "4/16/1990", "4/17/1990", "4/18/1990",
"4/19/1990", "4/20/1990", "4/21/1990", "4/22/1990", "4/23/1990",
"4/24/1990", "4/25/1990", "4/26/1990", "4/27/1990", "4/28/1990",
"4/29/1990", "4/30/1990", "5/1/1990", "5/2/1990", "5/3/1990",
"5/4/1990", "5/5/1990", "5/6/1990", "5/7/1990", "5/8/1990", "5/9/1990",
"5/10/1990", "5/11/1990", "5/12/1990", "5/13/1990", "5/14/1990",
"5/15/1990", "5/16/1990", "5/17/1990", "5/18/1990", "5/19/1990",
"5/20/1990", "5/21/1990", "5/22/1990", "5/23/1990", "5/24/1990",
"5/25/1990", "5/26/1990", "5/27/1990", "5/28/1990", "5/29/1990",
"5/30/1990", "5/31/1990", "6/1/1990", "6/2/1990", "6/3/1990",
"6/4/1990", "6/5/1990", "6/6/1990", "6/7/1990", "6/8/1990", "6/9/1990",
"6/10/1990", "6/11/1990", "6/12/1990", "6/13/1990", "6/14/1990",
"6/15/1990", "6/16/1990", "6/17/1990", "6/18/1990", "6/19/1990",
"6/20/1990", "6/21/1990", "6/22/1990", "6/23/1990", "6/24/1990",
"6/25/1990", "6/26/1990", "6/27/1990", "6/28/1990", "6/29/1990",
"6/30/1990", "7/1/1990", "7/2/1990", "7/3/1990", "7/4/1990",
"7/5/1990", "7/6/1990", "7/7/1990", "7/8/1990", "7/9/1990", "7/10/1990",
"7/11/1990", "7/12/1990", "7/13/1990", "7/14/1990", "7/15/1990",
"7/16/1990", "7/17/1990", "7/18/1990", "7/19/1990"), dia_da_semana = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L,
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
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 1990L, 1990L, 1990L), analise = c(0, 0, 0, 0, 100, 100,
100, 100, 100, 100, 100, 78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 100, 56.01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100, 100, 100, 100, 100, 100, 36.23, 0, 0, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 59.74, 0, 0, 0, 0, 100, 78, 0,
0, 0, 0, 0, 0, 0, 0, 100, 100, 100, 66.46, 100, 56.01, 0, 0,
0, 0, 100, 100, 72.23, 0, 0, 0, 0, 0, 100, 100, 100, 100, 78,
100, 100, 100, 44.46, 100, 100, 100, 100, 100, 100, 100, 59.74,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 56.01, 0, 0, 0, 0, 100,
100, 100, 54.97, 0, 0, 0, 0, 100, 56.01, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 100, 100, 75.35, 0, 0, 0)), row.names = c(NA,
200L), class = "data.frame")