0
I have a date.frame with the columns: cnpj_root, id_m_f, municipio and Uf
I need to check the amount of cnpj_root that exists in each municipality and check if the matrix (1) is in the same municipality of some branch (2).
What I’ve done so far:
library(tidyverse)
empresas %>%
group_by(cnpj_raiz, id_m_f, municipio) %>%
count()
Upshot:
cnpj_raiz id_m_f municipio n
1111111 1 PITIMBU 1
1111111 2 ALHANDRA 3
22222222 1 CARUARU 1
22222222 2 BREJO DA MADRE DE DEUS 4
33333333 1 SERRINHA 1
44444444 1 ARAPIRACA 1
55555555 1 FEIRA DE SANTANA 1
66666666 1 GUARAPUAVA 1
66666666 2 GUARAPUAVA 2
66666666 2 MOGI DAS CRUZES 3
For example, the cnpj_raiz==66666666
, possesses id_m_f
1 and 2 in the city of Guarapuava. How to make this identification?
I thought of a result similar to:
cnpj_raiz municipio n inclui_matriz
1111111 PITIMBU 1 F
1111111 ALHANDRA 3 F
22222222 CARUARU 1 F
22222222 BREJO DA MADRE DE DEUS 4 F
33333333 SERRINHA 1 F
44444444 ARAPIRACA 1 F
55555555 FEIRA DE SANTANA 1 F
66666666 GUARAPUAVA 3 T
66666666 MOGI DAS CRUZES 3 F
Thanks for the reply! I believe I am not able to explain well. Missed to check if the
cnpj_raiz
has matrix (id_m_f==1
) in the same municipality with a subsidiary (id_m_f==2
). Remembering that eachcnpj_raiz
has, necessarily, a head office and can have 0, 1 or several branches. For your example, if there are two branches in the same municipality, it can returncheck==sim
, right? It does not check whether it is affiliate or matrix summed inn()
. Once again, thank you for your attention!– RxT
In
id_m_f
I understood that1
represents matrix (M). and2
the subsidiary (F). As you want to see if M and F are in the same city, the first step I did was to check the city duplicity, independent of cnpj or M and F. So, as we do not have two M p/ a same cnpj, so if there is the same CNPJ p/ a municipality, it is understood that are M and F, considering what you want to analyze. Thecheck
was an extra step and can be deleted.– bbiasi
I get it, it’s true. Thank you!
– RxT