Reading CSV Data in Base R

Quick Overview

Write base R helpers that read CSV text or a CSV file into a data frame, report its dimensions, and compute the mean of every numeric column. It tests read.csv options, column type checks, and returning the right empty result when no column is numeric.

Reading CSV Data in Base R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement base R helpers that read comma-separated CSV data (the first row is the header) and summarise it. Character columns stay character and numeric columns stay numeric. - `read_csv_text(text)`: parse a single CSV string into a data.frame with `stringsAsFactors = FALSE`. - `read_csv_file(path)`: read the file into a data.frame with `stringsAsFactors = FALSE`, and raise an error if the file does not exist (the base `read.csv` behaviour). - `csv_dimensions(df)`: return `list(n_rows = <integer>, n_cols = <integer>)`. - `numeric_column_means(df)`: return a named numeric vector of means (`na.rm = TRUE`) for every numeric column, with names matching the source columns, or a length-0 named numeric vector if there are none. Example: ```r text <- "name,age,score\nAlice,30,9.5\nBob,25,8.0" df <- read_csv_text(text) csv_dimensions(df) # list(n_rows = 2L, n_cols = 3L) numeric_column_means(df) # c(age = 27.5, score = 8.75) ``` ```hint The no-numeric-columns case Try your means function on a data frame that has only character columns. Some ways of selecting and looping over columns return a list or lose the names when nothing matches. ``` ### Constraints & Assumptions - Base R only, no external packages. - Input is comma-separated, and the first row is always the header. ### What a Strong Answer Covers - Uses `read.csv` with `stringsAsFactors = FALSE` for both the string and the file, and lets a missing file raise base R's own error. - Returns the row and column counts as integers in a named list. - Selects numeric columns by type, counting both integer and double columns, and averages each with `na.rm = TRUE` while keeping the column names. - Returns a length-0 named numeric vector when no column is numeric, with no error or warning. ### Follow-up Questions - A column that should be numeric contains one cell reading `n/a`. What type does `read.csv` give that column, and how would you fix the import? - A CSV column is empty in every row. Does it appear in the output of `numeric_column_means`, and why? - How would you make `read.csv` faster on a file with millions of rows while staying in base R?

Overview: Write base R helpers that read CSV text or a CSV file into a data frame, report its dimensions, and compute the mean of every numeric column. It tests read.csv options, column type checks, and returning the right empty result when no column is numeric.

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

Implement base R helpers that read comma-separated CSV data (the first row is the header) and summarise it. Character columns stay character and numeric columns stay numeric.

  • read_csv_text(text) : parse a single CSV string into a data.frame with stringsAsFactors = FALSE .
  • read_csv_file(path) : read the file into a data.frame with stringsAsFactors = FALSE , and raise an error if the file does not exist (the base read.csv behaviour).
  • csv_dimensions(df) : return list(n_rows = <integer>, n_cols = <integer>) .
  • numeric_column_means(df) : return a named numeric vector of means ( na.rm = TRUE ) for every numeric column, with names matching the source columns, or a length-0 named numeric vector if there are none.

Example:

text <- "name,age,score\nAlice,30,9.5\nBob,25,8.0"
df <- read_csv_text(text)
csv_dimensions(df)        # list(n_rows = 2L, n_cols = 3L)
numeric_column_means(df)  # c(age = 27.5, score = 8.75)

Constraints & Assumptions

  • Base R only, no external packages.
  • Input is comma-separated, and the first row is always the header.

What a Strong Answer Covers Guidance

  • Uses read.csv with stringsAsFactors = FALSE for both the string and the file, and lets a missing file raise base R's own error.
  • Returns the row and column counts as integers in a named list.
  • Selects numeric columns by type, counting both integer and double columns, and averages each with na.rm = TRUE while keeping the column names.
  • Returns a length-0 named numeric vector when no column is numeric, with no error or warning.

Follow-up Questions Guidance

  • A column that should be numeric contains one cell reading n/a . What type does read.csv give that column, and how would you fix the import?
  • A CSV column is empty in every row. Does it appear in the output of numeric_column_means , and why?
  • How would you make read.csv faster on a file with millions of rows while staying in base R?