Command-line CSV Cleaner
Build an R script you run from the command line to clean up messy CSV files: Rscript clean.R messy.csv clean.csv. It trims extra spaces, makes text consistent (for example turning "bucharest", " Bucharest" and "BUCHAREST" into one spelling), converts dates written in different formats into one standard format, removes duplicate rows, and prints a summary of everything it changed. It's a great first R project outside RStudio: you'll learn writing standalone R scripts, reading command-line arguments, cleaning text with stringr, transforming data with dplyr, parsing dates, and reading and writing files with readr.
- Estimate
- ~6.5h
- Steps
- 5
- Completed by
- 0
- Proposed by
- codeseed.app
dplyr · stringr · readr
Project roadmap
- 01
Read the arguments
~1hWrite clean.R that reads the input and output file paths with commandArgs(trailingOnly = TRUE), prints a usage message and exits with an error code (quit(status = 1)) when they're missing or the input file doesn't exist. Load the file with readr::read_csv, reading every column as text at first, so nothing is guessed wrongly. Create a messy sample CSV to test with. Done when: the script loads the sample file and prints its number of rows and columns.
- 02
Clean up text
~1.5hFor every text column, use stringr to trim spaces at the ends (str_trim), collapse repeated spaces (str_squish), and turn empty strings and values like NA, n/a and - into missing values. Add an option for columns to convert to title case, such as city names. Count how many cells changed per column. Done when: the sample's messy text values become consistent.
- 03
Standardize dates and remove duplicates
~1.5hDetect date columns (by name or an option) and parse them with lubridate::parse_date_time trying several formats (such as 2026-03-15, 15/03/2026 and March 15, 2026), writing them as YYYY-MM-DD. Keep values that can't be parsed and report them. Then remove exact duplicate rows with dplyr::distinct, counting how many were removed. Done when: all dates in the sample share one format, and duplicates are gone.
- 04
Save and summarize
~1hWrite the result with readr::write_csv to the output path, refusing to overwrite the input file. Print a summary: rows before and after, duplicates removed, cells changed per column, and dates that couldn't be parsed with their row numbers. Done when: the cleaned file opens correctly, and the summary matches what changed.
- 05
Test it
~1.5hMove the cleaning steps into functions in a separate file, and write testthat tests for each: trimming and missing value handling, each date format, unparseable dates being kept and reported, and duplicate removal. Done when: all tests pass.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack