1. Import your data

Import two related datasets from TidyTuesday Project.

library(tidyverse)

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

Describe the two datasets:

Data1

Data 2

set.seed(1234)

companies_small <- companies %>% sample_n(5)
legal_nature_small <- legal_nature %>% sample_n(5)

companies_small
## # A tibble: 5 × 6
##   company_id company_name         legal_nature owner_qualification capital_stock
##        <dbl> <chr>                <chr>        <chr>                       <dbl>
## 1   59656511 CORACAO DO GIGANTE … Limited Lia… Administrator / Ma…       2000000
## 2   48688910 ONCA MINERACAO LTDA. Limited Lia… Administrator / Ma…      20000000
## 3   47271098 H E I  COMERCIO DE … Limited Lia… Managing Partner /…        300000
## 4   63195305 SOBERANA SUPERMERCA… Limited Lia… Managing Partner /…        500000
## 5   62189811 AUTO POSTO XAVANTE … Limited Lia… Managing Partner /…        300000
## # ℹ 1 more variable: company_size <chr>
legal_nature_small
## # A tibble: 5 × 2
##      id legal_nature                                
##   <dbl> <chr>                                       
## 1  3115 Mediation and Arbitration Entity            
## 2  3271 Local Governing Body of a Political Party   
## 3  3263 Regional Governing Body of a Political Party
## 4  1147 State or District Public Law Foundation     
## 5  2267 Simple Limited Partnership (Comandita)

3. inner_join

Describe the resulting data:

How is it different from the original two datasets? It combinded the rows that have matching legal nature keys in both datasets so only matching records are included

companies_small %>%
  inner_join(legal_nature_small, by = "legal_nature")
## # A tibble: 0 × 7
## # ℹ 7 variables: company_id <dbl>, company_name <chr>, legal_nature <chr>,
## #   owner_qualification <chr>, capital_stock <dbl>, company_size <chr>,
## #   id <dbl>

4. left_join

Describe the resulting data:

How is it different from the original two datasets? It keeps all rows from the companies dataset and adds matching information from the legal nature dataset

companies_small %>%
  left_join(legal_nature_small, by = "legal_nature")
## # A tibble: 5 × 7
##   company_id company_name         legal_nature owner_qualification capital_stock
##        <dbl> <chr>                <chr>        <chr>                       <dbl>
## 1   59656511 CORACAO DO GIGANTE … Limited Lia… Administrator / Ma…       2000000
## 2   48688910 ONCA MINERACAO LTDA. Limited Lia… Administrator / Ma…      20000000
## 3   47271098 H E I  COMERCIO DE … Limited Lia… Managing Partner /…        300000
## 4   63195305 SOBERANA SUPERMERCA… Limited Lia… Managing Partner /…        500000
## 5   62189811 AUTO POSTO XAVANTE … Limited Lia… Managing Partner /…        300000
## # ℹ 2 more variables: company_size <chr>, id <dbl>

5. right_join

Describe the resulting data:

How is it different from the original two datasets? It keeps all rows from the legal_nature dataset and adds matching information from the companies dataset

companies_small %>%
  right_join(legal_nature_small, by = "legal_nature")
## # A tibble: 5 × 7
##   company_id company_name legal_nature         owner_qualification capital_stock
##        <dbl> <chr>        <chr>                <chr>                       <dbl>
## 1         NA <NA>         Mediation and Arbit… <NA>                           NA
## 2         NA <NA>         Local Governing Bod… <NA>                           NA
## 3         NA <NA>         Regional Governing … <NA>                           NA
## 4         NA <NA>         State or District P… <NA>                           NA
## 5         NA <NA>         Simple Limited Part… <NA>                           NA
## # ℹ 2 more variables: company_size <chr>, id <dbl>

6. full_join

Describe the resulting data:

How is it different from the original two datasets? It combines all rows from both datasets

companies_small %>%
  full_join(legal_nature_small, by = "legal_nature")
## # A tibble: 10 × 7
##    company_id company_name        legal_nature owner_qualification capital_stock
##         <dbl> <chr>               <chr>        <chr>                       <dbl>
##  1   59656511 CORACAO DO GIGANTE… Limited Lia… Administrator / Ma…       2000000
##  2   48688910 ONCA MINERACAO LTD… Limited Lia… Administrator / Ma…      20000000
##  3   47271098 H E I  COMERCIO DE… Limited Lia… Managing Partner /…        300000
##  4   63195305 SOBERANA SUPERMERC… Limited Lia… Managing Partner /…        500000
##  5   62189811 AUTO POSTO XAVANTE… Limited Lia… Managing Partner /…        300000
##  6         NA <NA>                Mediation a… <NA>                           NA
##  7         NA <NA>                Local Gover… <NA>                           NA
##  8         NA <NA>                Regional Go… <NA>                           NA
##  9         NA <NA>                State or Di… <NA>                           NA
## 10         NA <NA>                Simple Limi… <NA>                           NA
## # ℹ 2 more variables: company_size <chr>, id <dbl>

7. semi_join

Describe the resulting data:

How is it different from the original two datasets? It only keeps the company rows that have a matching legal nature key in the second dataset

companies_small %>%
  semi_join(legal_nature_small, by = "legal_nature")
## # A tibble: 0 × 6
## # ℹ 6 variables: company_id <dbl>, company_name <chr>, legal_nature <chr>,
## #   owner_qualification <chr>, capital_stock <dbl>, company_size <chr>

8. anti_join

Describe the resulting data:

How is it different from the original two datasets? It keeps the company rows that dont have a matching legal nature key in the second dataset

companies_small %>%
  anti_join(legal_nature_small, by = "legal_nature")
## # A tibble: 5 × 6
##   company_id company_name         legal_nature owner_qualification capital_stock
##        <dbl> <chr>                <chr>        <chr>                       <dbl>
## 1   59656511 CORACAO DO GIGANTE … Limited Lia… Administrator / Ma…       2000000
## 2   48688910 ONCA MINERACAO LTDA. Limited Lia… Administrator / Ma…      20000000
## 3   47271098 H E I  COMERCIO DE … Limited Lia… Managing Partner /…        300000
## 4   63195305 SOBERANA SUPERMERCA… Limited Lia… Managing Partner /…        500000
## 5   62189811 AUTO POSTO XAVANTE … Limited Lia… Managing Partner /…        300000
## # ℹ 1 more variable: company_size <chr>