Skip to content

Preserve CSV identifiers with leading zeros in R

Mark identifier columns as character when you import CSVs with base R. In this three-row example, the default import keeps three rows but reduces three distinct site IDs to two.

Read the same three rows two ways

The fictional site records use 001, 1, and 010 as three different IDs. Save the CSV and run these commands from the directory where you downloaded it.

inferred <- read.csv("csv-identifier-check.csv")
preserved <- read.csv(
  "csv-identifier-check.csv",
  colClasses = c(ExternalID = "character")
)

inferred$ExternalID
# [1]  1  1 10
preserved$ExternalID
# [1] "001" "1"   "010"

Both imports return three rows. The default import has only two distinct keys and marks a second occurrence of 1. Declaring just ExternalID as character preserves all three keys and leaves other columns to normal inference. See the base R import documentation.

Test a real CSV of California school codes

The California Department of Education data dictionary defines the County-District-School code as a 14-character identifier. Its first two characters identify the county.

We imported the 2023–24 California Public Schools CSV twice in base R 4.6.0 and compared the resulting codes with the original text keys. The two reads each returned 9,996 distinct codes. With inferred numeric types, 867 lost a leading zero and could no longer match the original keys. The character import preserved all 9,996. For example, 01100170112607 became 1100170112607 in the numeric read.

Download the official CSV and the base R check for school codes. Pass the downloaded CSV as the argument to the script and compare the unmatched count for your copy.

Rscript check-california-school-keys.R schools.csv

Test a CSV with quoted identifiers

After running the commands above, export preserved to a new CSV. In a text editor, each ID should appear in double quotes as "001", "1", or "010".

write.csv(preserved, "quoted-sites.csv", row.names = FALSE)
read.csv("quoted-sites.csv")$ExternalID
# [1]  1  1 10
read.csv(
  "quoted-sites.csv",
  colClasses = c(ExternalID = "character")
)$ExternalID
# [1] "001" "1"   "010"

In base R 4.6.0, both default imports reduce three distinct IDs to two, with or without quotes. DuckDB 2.0.1 read_csv() inferred VARCHAR for the same column in both files and preserved all three IDs. When sharing CSVs between tools, declare the identifier column as text at import and compare the resulting keys with the source before joining.

Check whether validation or conversion changed the IDs

Using the same three-row CSV in the DeciMethod Data Quality & Preparation Lab, we declared ExternalID as Integer. All three values passed the type check. The prepared data still contained 001, 1, and 010. Applying a conversion factor of 2 to the same column produced 2, 2, and 20.

DeciMethod Data Quality Lab 1.1.0, December 2, 2026
ExternalID ruleIDs in the prepared CSVDistinct IDsRepeated key groups
Text001, 1, 01030
Integer, factor 1001, 1, 01030
Integer, factor 22, 2, 2021
  1. Paste the sample CSV into Original CSV, enter a project title, and choose Read columns & preview.
  2. Select ExternalID under Key columns. Keep Keep every row for duplicates. Run the preparation preview with ExternalID set to Text, then again with Integer.
  3. Open Numeric range and unit conversion for that column. Set Conversion factor to 2 as a test, run the preview, and check the reported repeated key. Download Prepared CSV and Audit JSON.

All three runs kept three rows. In the first two runs, Prepared CSV and Spreadsheet-safe CSV had identical bytes. We re-imported those files in R 4.6.0. With column types inferred, both exports gave 1, 1, 10. Declaring ExternalID as character at import kept 001, 1, 010.

Join payments and verify where each one lands

Save both downloads in the same directory and run the script with base R. It joins payments of £25, £75, and £50 to site IDs 001, 1, and 999. The last payment has no site match and stays in the left join.

Rscript check-r-csv-identifiers.R
Results in R 4.6.0
Keys usedJoined rowsPayment totalSite matched for the £25 payment
Numeric keys on both sides5£250East branch and North workshop
Character keys from import3£150North workshop
Payment IDs cast back to character3£150East branch

With numeric keys on both sides, two payments each match two sites. Casting the payment keys to character after conversion restores the row count and total, but the £25 payment still matches East branch. Read the original ID as character on both sides, then compare the joined site for each PaymentID as well as the totals.

Spreadsheet to App ran this example in base R through webR with Codex. All rows are fictional. You can also run the same join in DuckDB.