I have the following dataset:
id_municipio ano CLARO OI TIM VIVO
1 1300029 1999 0 5 0 0
2 1300029 2006 0 2 0 0
3 1300029 2012 0 0 0 2
4 1300029 2015 0 2 0 0
5 1300029 2016 2 0 0 2
6 1300029 2022 0 0 1 0
I want to sum "CLARO", "OI", "TIM", and "VIVO" by "id_municipio", and "ano". So, I would have the following panel:
id_municipio ano CLARO OI TIM VIVO
1 1300029 1999 0 5 0 0
2 1300029 2006 0 7 0 0
3 1300029 2012 0 7 0 2
4 1300029 2015 0 9 0 2
5 1300029 2016 2 9 0 4
6 1300029 2022 2 9 1 4
I know it should be something like this, but I keep getting the original dataset. What am I doing wrong?
teste_3 <- teste_2 %>%
group_by(id_municipio, ano) %>%
arrange(ano) %>%
mutate(
CLARO = cumsum(CLARO),
OI = cumsum(OI),
TIM = cumsum(TIM),
VIVO = cumsum(VIVO)
)
CodePudding user response:
I could manage to solve this issue by dropping "ano" from the code.
teste_3 <- teste_2 %>%
group_by(id_municipio, ano) %>%
arrange(ano) %>%
mutate(
CLARO = cumsum(CLARO),
OI = cumsum(OI),
TIM = cumsum(TIM),
VIVO = cumsum(VIVO)
)