0

Please, I want to repeat in R a data crossing I did in Python 3 - Pandas. The most appropriate way would be with the use of dplyr?

I have two CSVs. One is about donations for politicians: each line is a donation and each line has the name of the donor company and the federal registration number of that company (CPF_CNPJ_doador - unique for each company in the country)

CPF_CNPJ_doador Nome_doador                               Valor
73668675000187  CWVGRAFICA EDITORA E BAZAR EIRELI LTDA ME 1000,00

83647909000163  CARBONIFERA CRICIUMA S/A 1750,00

10731057000114  HAROLDO AZEVEDO CONSTRUÇÕES LTDA 100,00
...

The other CSV has companies that have environmental irregularities. You have several data, including the company name and the federal registration number of that company (CPF_CNPJ - unique for each company in the country). But a company may appear more than once in this CSV - it may have multiple affiliates with irregularities for example

Nome_Razao_Social   CPF_CNPJ
Carajas Madeiras Industria e Comercio Ltda - Me 02579504000214
Carbo Gás Ltda  03828695000435
Carbomil Química S/A    07645062000108
Carbomil Química S/A    07645062000108

I made a pandas merge (Python 3) of these two CSVs:

ibama_doadores_orig= pd.merge(eleitos_d_doadores, ibama.drop_duplicates('CPF_CNPJ_limpo'), left_on='CPF_CNPJ_doador_originario', right_on='CPF_CNPJ_limpo', how = 'left')

And then I removed the nulls

ibama_doadores_orig = ibama_doadores_orig[pd.notnull(ibama_doadores_orig['CPF_CNPJ_limpo'])]

Both can be duplicated in CSVs (a politician can receive donations from the same company on different dates, for example, and a company number repeated in the second csv).

But I want to get all the lines of donations, even if they are from lines with duplicate companies. But from the second CSV I just want to get the first appearance

In Python this worked (I checked the Excel PivotTable). Please, in the R language, does anyone know how to do it?

Data example

First lines of the ibama file:

    index   Nome_Razao_Social   CPF_CNPJ    CPF_CNPJ_limpo  UF_Embargo  Municipio_Embargo   Infracao    Data_Lista
0   0   Leandro Polli Ramos 050.153.549-75  5.015355e+09    SC  Passos Maia Vender, expor à venda, ter em depósito, transp...   10/07/2012
1   1   Antonio Marrocos Filho  046.465.921-34  4.646592e+09    GO  Cocalzinho de Goiás Executar pesquisa, lavra ou extração de recurs...   28/02/2007
2   2   Nilton Garcia Rodrigues 057.743.069-68  5.774307e+09    RO  Candeias do Jamari  Infração da Flora(Não Classificada-Móvel)   04/04/2017

First lines of the file eleitos_d_doadores:

index   UF  Partido_x   Cargo_x Nome_candidato_x    CPF_candidato   CPF_CNPJ_doador Nome_doador Nome_doador_Receita Valor   CPF_CNPJ_doador_originario  Nome_doador_originario  Nome_doador_originario_Receita  Estado  Cargo_y Nome_candidato_y    CPF Nome_urna   Partido_y   Situacao
0   0   BA  PRB Deputado Federal    ERONILDES VASCONCELOS CARVALHO  66858933549 2.056688e+13    ELEIÇÃO 2014 SIDELVAN DE ALMEIDA NOBREGA DEPUT...   ELEICAO 2014 SIDELVAN DE ALMEIDA NOBREGA DEPUT...   575 11055596534 CARMEM LUCIA N MAGALHÃES    CARMEN LUCIA MEIRA MAGALHAES    BAHIA   DEPUTADO FEDERAL    ERONILDES VASCONCELOS CARVALHO  66858933549 TIA ERON    PRB ELEITO POR QP
1   1   BA  PRB Deputado Federal    ERONILDES VASCONCELOS CARVALHO  66858933549 2.056688e+13    ELEIÇÃO 2014 SIDELVAN DE ALMEIDA NOBREGA DEPUT...   ELEICAO 2014 SIDELVAN DE ALMEIDA NOBREGA DEPUT...   2079,2  29369070559 GERALDO NASCIMENTO SANTOS   GERALDO NASCIMENTO SANTOS   BAHIA   DEPUTADO FEDERAL    ERONILDES VASCONCELOS CARVALHO  66858933549 TIA ERON    PRB ELEITO POR QP
2   2   BA  PRB Deputado Federal    ERONILDES VASCONCELOS CARVALHO  66858933549 2.056688e+13    ELEIÇÃO 2014 SIDELVAN DE ALMEIDA NOBREGA DEPUT...   ELEICAO 2014 SIDELVAN DE ALMEIDA NOBREGA DEPUT...   3900    

Merge fields: CPF_CNPJ_limpo and CPF_CNPJ_original

Python end result:

,UF,Partido_x,Cargo_x,Nome_candidato_x,CPF_candidato,CPF_CNPJ_doador,Nome_doador,Nome_doador_Receita,Valor,CPF_CNPJ_doador_originario,Nome_doador_originario,Nome_doador_originario_Receita,Estado,Cargo_y,Nome_candidato_y,CPF,Nome_urna,Partido_y,Situacao,Nome_Razao_Social,CPF_CNPJ,CPF_CNPJ_limpo,UF_Embargo,Municipio_Embargo,Infracao,Data_Lista
87,RS,PP,Deputado Federal,JOSÉ ALFONSO EBERT HAMM,37040642034,887169000105.0,Direção Nacional,PARTIDO PROGRESSISTA,50000,66806555000133.0,FBS CONSTRUÇÃO CIVIL E PAVIMENTAÇÃO LTDA,FBS CONSTRUCAO CIVIL E PAVIMENTACAO S.A.,RIO GRANDE DO SUL,DEPUTADO FEDERAL,JOSÉ ALFONSO EBERT HAMM,37040642034,AFONSO HAMM,PP,ELEITO POR QP,Fbs Construcao Civil e Pavimentacao Ltda,66.806.555/0001-33,66806555000133.0,CE,Quixeramobim,"Executar pesquisa, lavra ou extração de minerais sem a competente autorização, permissão, concessão ou licença da autoridade ambiental competente ou em desacordo com a obtida.",11/10/2010
358,SC,PT,Deputado Federal,DÉCIO NERY DE LIMA,38858240944,79306908000188.0,Direção Estadual/Distrital,PARTIDO DOS TRABALHADORES DE SANTA CATARINA,350000,1317277000105.0,ITAPOÁ TERMINAIS PORTUÁRIOS SA,ITAPOA TERMINAIS PORTUARIOS S/A,SANTA CATARINA,DEPUTADO FEDERAL,DÉCIO NERY DE LIMA,38858240944,DÉCIO LIMA,PT,ELEITO POR QP,Itapoá Terminais Portuários Sa,01.317.277/0001-05,1317277000105.0,SC,Itapoá,"Construir, reformar, ampliar, instalar ou fazer funcionar estabelecimentos, obras ou serviços potencialmente poluidores ou utilizadores de recursos naturais, sem licença ou autorização dos órgãos ambientais competentes, em desacordo com a licença obtida ou contrariando as normas legais e regulamentos pertinentes.",22/10/2010
365,SC,PT,Deputado Federal,DÉCIO NERY DE LIMA,38858240944,79306908000188.0,Direção Estadual/Distrital,PARTIDO DOS TRABALHADORES DE SANTA CATARINA,50000,76614254000161.0,SUL CATARINENSE MINERAÇÃO ARTEFATOS DE CIMENTOS BRITAGEM E CONSTRUÇÕES LTDA,SULCATARINENSE MIN ARTEF DE CIM BRIT E CONSTRUCOES LTDA,SANTA CATARINA,DEPUTADO FEDERAL,DÉCIO NERY DE LIMA,38858240944,DÉCIO LIMA,PT,ELEITO POR QP,Sulcatarinense - Min. Art. Cim. Brit. e Cont. Ltda,76.614.254/0001-61,76614254000161.0,SC,Biguaçu,"Executar pesquisa, lavra ou extração de recursos minerais sem a competente licença ambiental ou em desacordo com a mesma.",22/12/2008
Reinaldo Chaves
  • 965
  • 4
  • 16
  • 43
  • 2
    Did you try any R code? Here exactly did you get stuck? R has a `merge()` function too. – MrFlick Oct 20 '17 at 17:50
  • Yes, but it repeats donation lines: ibama_doadores = merge(eleitos_d_doadores, ibama_l, by.x = 'CPF_CNPJ_doador', by.y = 'CPF_CNPJ_limpo') – Reinaldo Chaves Oct 20 '17 at 17:56
  • 1
    Since your question is about R, then it would help if you provided a proper [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with your data in an R format (what you have doesn't even look like CSV given there are no commas). Show the desired output for your sample input. – MrFlick Oct 20 '17 at 18:02
  • Sure, thanks. I put it up but do not know how to format it correctly on the screen – Reinaldo Chaves Oct 20 '17 at 18:17

0 Answers0