Combine every CSV file in a directory into one R data frame

Quick Overview

Write two R helpers that read every CSV file in a directory and stack them into one data frame, optionally tagging each row with its source file. It tests file listing with regular expressions, deterministic ordering, bind_rows column alignment and handling of empty directories and header-only files.

Combine every CSV file in a directory into one R data frame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement two R helpers that read every CSV file in a directory and combine them into one data frame, optionally tagging each row with the file it came from. - `read_one_csv(path, add_source = FALSE)` reads the file with `read.csv(stringsAsFactors = FALSE)`. If `add_source` is `TRUE`, it appends a character column `source_file` holding `basename(path)`. - `bind_csv_dir(dir, add_source = FALSE)` finds the files in `dir` whose names match `"\\.csv$"` (case-sensitive), sorted alphabetically by full path. - It reads each file with `read_one_csv()`, passing `add_source` through, and combines them with `dplyr::bind_rows()`, so columns missing from a file are filled with `NA`. - If the directory has no CSV files, it returns `data.frame()` (0 rows, 0 columns). Example: ```r # dir/a.csv -> id,name # 1,ann # dir/b.csv -> id,name # 2,bob bind_csv_dir("dir", add_source = TRUE) # id name source_file # 1 1 ann a.csv # 2 2 bob b.csv ``` ```hint Small cases first Before writing the general case, decide what your code does with an empty directory, a file that has a header row but no data rows, and two files whose columns differ. ``` ### Constraints & Assumptions - Each CSV is comma-separated with a header row. - Only files directly inside `dir` are read, not files in subdirectories. - `dplyr` is available. ### What a Strong Answer Covers - The file listing matches only names ending in `.csv`, case-sensitively, and returns them in a deterministic sorted order. - Each file is read with `stringsAsFactors = FALSE`, and `source_file` is a character column of base names when requested. - Files with different column sets are combined by column name, with `NA` where a file lacks a column. - The empty directory returns `data.frame()` without an error, and the answer discusses how a header-only file behaves, both alone and in a directory where other files have data rows. ### Follow-up Questions - How would you rewrite `bind_csv_dir()` using `purrr`? - How would you handle a directory too large to fit in memory? - What happens when the same column holds integers in one file and text in another, and how would you resolve it?

Overview: Write two R helpers that read every CSV file in a directory and stack them into one data frame, optionally tagging each row with its source file. It tests file listing with regular expressions, deterministic ordering, bind_rows column alignment and handling of empty directories and header-only files.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement two R helpers that read every CSV file in a directory and combine them into one data frame, optionally tagging each row with the file it came from.

  • read_one_csv(path, add_source = FALSE) reads the file with read.csv(stringsAsFactors = FALSE) . If add_source is TRUE , it appends a character column source_file holding basename(path) .
  • bind_csv_dir(dir, add_source = FALSE) finds the files in dir whose names match "\\.csv$" (case-sensitive), sorted alphabetically by full path.
  • It reads each file with read_one_csv() , passing add_source through, and combines them with dplyr::bind_rows() , so columns missing from a file are filled with NA .
  • If the directory has no CSV files, it returns data.frame() (0 rows, 0 columns).

Example:

# dir/a.csv -> id,name
#             1,ann
# dir/b.csv -> id,name
#             2,bob

bind_csv_dir("dir", add_source = TRUE)
#   id name source_file
# 1  1  ann       a.csv
# 2  2  bob       b.csv

Constraints & Assumptions

  • Each CSV is comma-separated with a header row.
  • Only files directly inside dir are read, not files in subdirectories.
  • dplyr is available.

What a Strong Answer Covers Guidance

  • The file listing matches only names ending in .csv , case-sensitively, and returns them in a deterministic sorted order.
  • Each file is read with stringsAsFactors = FALSE , and source_file is a character column of base names when requested.
  • Files with different column sets are combined by column name, with NA where a file lacks a column.
  • The empty directory returns data.frame() without an error, and the answer discusses how a header-only file behaves, both alone and in a directory where other files have data rows.

Follow-up Questions Guidance

  • How would you rewrite bind_csv_dir() using purrr ?
  • How would you handle a directory too large to fit in memory?
  • What happens when the same column holds integers in one file and text in another, and how would you resolve it?