Sorry, I don't know how to generalize my question or improve the title.
But, my problem is:
I have this structure
firm <- c(11212, 11212,11212,11212, 2632, 2632, 3552, 4218, 536, 6667, 6667, 6667, 7278, 7278)
id_p_s <- c(1, 2, 2,2, 1, 2, 1, 1, 1, 1, 2, 2, 1, 2)
state <- c("SP", "MG","RJ","RJ", "PI", "ES","PI", "PI", "ES", "ES",
"SP", "SP", "MG", "ES")
enterprises <- data.frame(firm = firm, id_p_s = id_p_s,
state = state)
If the firm is a parent, then id_p_s==1
otherwise is 2
. Every firm has 1 parent and can has 0, 1 or more subsidiaries, which can be in the same or in another State.
I need to see how many firms (unique code) has in the state and the states it has subsidiaries (unique code peer firm).
Something like this:
SP MG RJ ES PI
SP 1 1 1 0 0
MG 0 1 0 1 0
RJ 0 0 0 0 0
ES 1 0 0 2 0
PI 0 0 0 1 3
PI has 3 parent firms, but only one has subsidiary in ES. In SP I have 1 parent firm with subsidiary in MG and RJ.
Thank so much for you atention!