Back to Practice
#0050

Left Join with merge

MediumR15 min15 XP

Problem

Given orders and customers data frames, attach customer names to orders. Keep every order even if the customer is missing from the lookup table.

Why This Matters

Joining is where analysis silently loses rows. In R, knowing all.x = TRUE prevents accidental inner joins.

Function Signature

merge(orders, customers, by = "customer_id", all.x = TRUE)

Examples

Example 1
Inputorders include customer 99, customers table does not
Outputthree order rows; customer 99 has NA name

all.x = TRUE keeps every row from the first data frame.

Constraints

  • Use base R merge.
  • Keep every order row.
  • Join using customer_id.
CodeR
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Unknown customer remains
Inputthree orders, two known customers
Expectedthree rows after merge

The unknown customer should not disappear.

Hidden Test Categories
All customers knownNo customers knownRepeated customer IDs in orders