1. Import your data

Import two related datasets from TidyTuesday Project.

# csv files
companies <- read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2026/2026-01-27/companies.csv')
## Rows: 141332 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): company_name, legal_nature, owner_qualification, company_size
## dbl (2): company_id, capital_stock
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
legal_nature <- read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2026/2026-01-27/legal_nature.csv')
## Rows: 91 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): legal_nature
## dbl (1): id
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

2. Make data small

set.seed(1234)
companies_small <- companies %>% select(company_name, legal_nature, company_size) %>% sample_n(50)
legal_small <- legal_nature %>% select(id, legal_nature) %>% sample_n(50)

companies_small
## # A tibble: 50 × 3
##    company_name                                        legal_nature company_size
##    <chr>                                               <chr>        <chr>       
##  1 CORACAO DO GIGANTE PARTICIPACOES LTDA               Limited Lia… other       
##  2 ONCA MINERACAO LTDA.                                Limited Lia… other       
##  3 H E I  COMERCIO DE CEREAIS LTDA                     Limited Lia… small-enter…
##  4 SOBERANA SUPERMERCADO LTDA                          Limited Lia… small-enter…
##  5 AUTO POSTO XAVANTE LTDA                             Limited Lia… other       
##  6 BAIXO ARAGUAIA BUZIOS LTDA                          Limited Lia… small-enter…
##  7 FLORICULTURA ROSA DE SAARON LTDA                    Limited Lia… small-enter…
##  8 GARTONI CONFECCOES LTDA                             Limited Lia… micro-enter…
##  9 OURO VERDE C.L EMPREENDIMENTOS IMOBILIARIOS S.A     Privately H… other       
## 10 CTR AMBIENTAL CENTRO DE TRATAMENTO DE RESIDUOS LTDA Limited Lia… small-enter…
## # ℹ 40 more rows
legal_small
## # A tibble: 50 × 2
##       id legal_nature                                   
##    <dbl> <chr>                                          
##  1  1147 State or District Public Law Foundation        
##  2  1244 Municipality                                   
##  3  2305 Individual Limited Liability Company (Business)
##  4  1198 Multinational Commission                       
##  5  5010 International Organization                     
##  6  3131 Trade Union / Labor Union                      
##  7  3212 Foreign-Domiciled Foundation or Association    
##  8  5029 Foreign Diplomatic Representation              
##  9  2160 Group of Companies                             
## 10  1279 Municipal Private Law Public Foundation        
## # ℹ 40 more rows

Describe the two datasets:

Data1

Data 2

3. inner_join

inner <- companies_small %>% inner_join(legal_small, by = "legal_nature")
inner
## # A tibble: 4 × 4
##   company_name                                   legal_nature company_size    id
##   <chr>                                          <chr>        <chr>        <dbl>
## 1 OURO VERDE C.L EMPREENDIMENTOS IMOBILIARIOS S… Privately H… other         2054
## 2 MERIDIAN PARTICIPACOES S/A.                    Privately H… other         2054
## 3 CENTRAL FOTOVOLTAICA BOA SORTE 10 SPE S.A.     Privately H… other         2054
## 4 ATMO CONCEPT HOME SPE S.A.                     Privately H… other         2054

Describe the resulting data:

How is it different from the original two datasets?

Only companies whose legal nature is also in the sampled lookup table are kept, and the lookup’s id is added as a new column.

4. left_join

left <- companies_small %>% left_join(legal_small, by = "legal_nature")
left
## # A tibble: 50 × 4
##    company_name                                  legal_nature company_size    id
##    <chr>                                         <chr>        <chr>        <dbl>
##  1 CORACAO DO GIGANTE PARTICIPACOES LTDA         Limited Lia… other           NA
##  2 ONCA MINERACAO LTDA.                          Limited Lia… other           NA
##  3 H E I  COMERCIO DE CEREAIS LTDA               Limited Lia… small-enter…    NA
##  4 SOBERANA SUPERMERCADO LTDA                    Limited Lia… small-enter…    NA
##  5 AUTO POSTO XAVANTE LTDA                       Limited Lia… other           NA
##  6 BAIXO ARAGUAIA BUZIOS LTDA                    Limited Lia… small-enter…    NA
##  7 FLORICULTURA ROSA DE SAARON LTDA              Limited Lia… small-enter…    NA
##  8 GARTONI CONFECCOES LTDA                       Limited Lia… micro-enter…    NA
##  9 OURO VERDE C.L EMPREENDIMENTOS IMOBILIARIOS … Privately H… other         2054
## 10 CTR AMBIENTAL CENTRO DE TRATAMENTO DE RESIDU… Limited Lia… small-enter…    NA
## # ℹ 40 more rows

Describe the resulting data:

How is it different from the original two datasets?

All 50 companies are kept and id is added. Companies whose legal nature is not in the sampled lookup table get NA for id.

5. right_join

right <- companies_small %>% right_join(legal_small, by = "legal_nature")
right
## # A tibble: 53 × 4
##    company_name                                  legal_nature company_size    id
##    <chr>                                         <chr>        <chr>        <dbl>
##  1 OURO VERDE C.L EMPREENDIMENTOS IMOBILIARIOS … Privately H… other         2054
##  2 MERIDIAN PARTICIPACOES S/A.                   Privately H… other         2054
##  3 CENTRAL FOTOVOLTAICA BOA SORTE 10 SPE S.A.    Privately H… other         2054
##  4 ATMO CONCEPT HOME SPE S.A.                    Privately H… other         2054
##  5 <NA>                                          State or Di… <NA>          1147
##  6 <NA>                                          Municipality <NA>          1244
##  7 <NA>                                          Individual … <NA>          2305
##  8 <NA>                                          Multination… <NA>          1198
##  9 <NA>                                          Internation… <NA>          5010
## 10 <NA>                                          Trade Union… <NA>          3131
## # ℹ 43 more rows

Describe the resulting data:

How is it different from the original two datasets?

All rows of the lookup table are kept. Legal natures with no sampled company get NA for company_name and company_size, and a legal nature used by several companies appears once per company.

6. full_join

full <- companies_small %>% full_join(legal_small, by = "legal_nature")
full
## # A tibble: 99 × 4
##    company_name                                  legal_nature company_size    id
##    <chr>                                         <chr>        <chr>        <dbl>
##  1 CORACAO DO GIGANTE PARTICIPACOES LTDA         Limited Lia… other           NA
##  2 ONCA MINERACAO LTDA.                          Limited Lia… other           NA
##  3 H E I  COMERCIO DE CEREAIS LTDA               Limited Lia… small-enter…    NA
##  4 SOBERANA SUPERMERCADO LTDA                    Limited Lia… small-enter…    NA
##  5 AUTO POSTO XAVANTE LTDA                       Limited Lia… other           NA
##  6 BAIXO ARAGUAIA BUZIOS LTDA                    Limited Lia… small-enter…    NA
##  7 FLORICULTURA ROSA DE SAARON LTDA              Limited Lia… small-enter…    NA
##  8 GARTONI CONFECCOES LTDA                       Limited Lia… micro-enter…    NA
##  9 OURO VERDE C.L EMPREENDIMENTOS IMOBILIARIOS … Privately H… other         2054
## 10 CTR AMBIENTAL CENTRO DE TRATAMENTO DE RESIDU… Limited Lia… small-enter…    NA
## # ℹ 89 more rows

Describe the resulting data:

How is it different from the original two datasets?

Everything from both tables is kept, with NA wherever there is no match, so it has at least as many rows as either original table.

7. semi_join

semi <- companies_small %>% semi_join(legal_small, by = "legal_nature")
semi
## # A tibble: 4 × 3
##   company_name                                    legal_nature      company_size
##   <chr>                                           <chr>             <chr>       
## 1 OURO VERDE C.L EMPREENDIMENTOS IMOBILIARIOS S.A Privately Held C… other       
## 2 MERIDIAN PARTICIPACOES S/A.                     Privately Held C… other       
## 3 CENTRAL FOTOVOLTAICA BOA SORTE 10 SPE S.A.      Privately Held C… other       
## 4 ATMO CONCEPT HOME SPE S.A.                      Privately Held C… other

Describe the resulting data:

How is it different from the original two datasets?

Only the companies that have a match in the lookup table are kept, with the original 3 company columns and nothing added from legal_small.

8. anti_join

anti <- companies_small %>% anti_join(legal_small, by = "legal_nature")
anti
## # A tibble: 46 × 3
##    company_name                                        legal_nature company_size
##    <chr>                                               <chr>        <chr>       
##  1 CORACAO DO GIGANTE PARTICIPACOES LTDA               Limited Lia… other       
##  2 ONCA MINERACAO LTDA.                                Limited Lia… other       
##  3 H E I  COMERCIO DE CEREAIS LTDA                     Limited Lia… small-enter…
##  4 SOBERANA SUPERMERCADO LTDA                          Limited Lia… small-enter…
##  5 AUTO POSTO XAVANTE LTDA                             Limited Lia… other       
##  6 BAIXO ARAGUAIA BUZIOS LTDA                          Limited Lia… small-enter…
##  7 FLORICULTURA ROSA DE SAARON LTDA                    Limited Lia… small-enter…
##  8 GARTONI CONFECCOES LTDA                             Limited Lia… micro-enter…
##  9 CTR AMBIENTAL CENTRO DE TRATAMENTO DE RESIDUOS LTDA Limited Lia… small-enter…
## 10 61.839.939 ADILSON JOSE CALDAS                      Sole Propri… micro-enter…
## # ℹ 36 more rows

Describe the resulting data:

How is it different from the original two datasets?

Only the companies with NO match in the lookup table are kept, with just the 3 company columns. It is the opposite of semi_join, so semi_join and anti_join rows add up to 50.