Skip to content

Join two tables in R and keep the unmatched rows visible

Draft: not yet validated

The code on this page was run inside MESA RStudio Geospatial on 2026-11-16 (R 4.6.2), by the AI assistant that drafted it, and produced the output shown. No person has yet followed the steps, so a screen may differ. Tell us what you find.

merge() keeps only the rows that find a match. The rest leave without a message, and every count after the join is quietly smaller. all.x = TRUE keeps them where you can see them.

You will: have a join in which a gauge with no position is still listed, and a count of how many gauges matched.

Time: 10 minutes. You need: the readings and gauges tables from Load a file from your Data Store in RStudio, in MESA RStudio Geospatial.

Steps

  1. In the Console, fetch the file of gauge positions into your practice folder:

    download.file("https://idss-mesa.github.io/trainings/assets/practice/gauge_sites.csv", "gauge_sites.csv")
    

    You should see: a line that reads downloaded 144 bytes.

  2. Read it, and make a copy that is incomplete on purpose: only the gauges west of one line of longitude.

    sites <- read.csv("gauge_sites.csv")
    west <- sites[sites$longitude < -104.73, ]
    nrow(west)
    

    You should see: four positions, for five gauges.

    [1] 4
    
  3. Join the gauges to those positions in the ordinary way:

    merge(gauges[c("gauge_id", "name")], west[c("gauge_id", "longitude", "latitude")], by = "gauge_id")
    

    You should see: four rows and no warning. One gauge has gone, and nothing says so.

      gauge_id      name longitude latitude
    1      G01 Boathouse -104.7612  47.1034
    2      G02   Orchard -104.7391  47.1108
    3      G04     Depot -104.7533  47.1175
    4      G05    Quarry -104.7306  47.0962
    
  4. Add all.x = TRUE, which keeps every row of the table named first:

    kept <- merge(gauges[c("gauge_id", "name")], west[c("gauge_id", "longitude", "latitude")], by = "gauge_id", all.x = TRUE)
    kept
    

    You should see: five rows. The gauge without a position is there, with NA where its position would be.

      gauge_id        name longitude latitude
    1      G01   Boathouse -104.7612  47.1034
    2      G02     Orchard -104.7391  47.1108
    3      G03 Observatory        NA       NA
    4      G04       Depot -104.7533  47.1175
    5      G05      Quarry -104.7306  47.0962
    
  5. Count the matches, so the answer does not depend on reading every row:

    c(gauges = nrow(kept), matched = sum(!is.na(kept$longitude)), unmatched = sum(is.na(kept$longitude)))
    

    You should see:

       gauges   matched unmatched
            5         4         1
    
  6. Run the same join and count against the complete table, sites:

    full <- merge(gauges[c("gauge_id", "name")], sites[c("gauge_id", "longitude", "latitude")], by = "gauge_id", all.x = TRUE)
    c(gauges = nrow(full), matched = sum(!is.na(full$longitude)), unmatched = sum(is.na(full$longitude)))
    

    You should see: every gauge matched.

       gauges   matched unmatched
            5         5         0
    

Check it worked

In each count, matched plus unmatched equals gauges. You can say which gauge had no position in step 4, and that the complete table leaves none out.

If it didn't

cannot open file 'gauge_sites.csv': No such file or directory. Step 1 saved the file somewhere else, because R is not in your practice folder. Run getwd() to see where it is, go to the folder with setwd() as in Load a file from your Data Store in RStudio, and repeat step 1.

object 'west' not found. R has restarted since step 2. Run step 2 again.

Next

Not yet validated by a person in MESA RStudio Geospatial. Drafted with AI assistance on 2026-11-16; the code was run there that day (R 4.6.2).

Machine-readable versions of this page: Markdown twin · raw source on GitHub · llms.txt · llms-full.txt (whole site). See For AI agents.