Reading and writing data

R · Practical · 10 min

Real work starts with data in files, and import options decide whether your columns come in right.

Base R reads delimited text with read.csv(path) (comma-separated, header row) and read.delim (tabs); read.table is the general version. Useful arguments: na.strings = c("", "NA") for how missing values are written, colClasses to force column types, skip and nrows. Write with write.csv(df, path, row.names = FALSE); without row.names = FALSE you get an extra unnamed column.

readRDS/saveRDS store any single R object in R's binary format, preserving types and factors exactly; use them for intermediate results.

In practice most people use packages:

  • readr (read_csv) is faster, returns a tibble, never converts strings to factors, and reports column types it guessed.
  • data.table's fread is extremely fast on large files and guesses separators.
  • readxl reads Excel files, haven SPSS/Stata/SAS, arrow Parquet, and DBI talks to databases.

Paths are relative to the working directory (getwd()); rather than setwd() in scripts, use project-relative paths (RStudio projects or the here package).

After importing, always check: str(df) for types, summary(df) for ranges and NA counts. A numeric column that came in as character usually means a stray value like "n/a" or "1,234".

Example

path <- tempfile(fileext = ".csv")
writeLines(c("id,city,temp", "1,Yerevan,31.5", "2,Gyumri,n/a", "3,Dilijan,24"), path)

df <- read.csv(path)
print(class(df$temp))                 #> [1] "character" (because of n/a)
df <- read.csv(path, na.strings = c("", "n/a"))
str(df)
#> 'data.frame':	3 obs. of  3 variables:
#>  $ id  : int  1 2 3
#>  $ city: chr  "Yerevan" "Gyumri" "Dilijan"
#>  $ temp: num  31.5 NA 24

out <- tempfile(fileext = ".csv")
write.csv(df[!is.na(df$temp), ], out, row.names = FALSE)
print(readLines(out)[1:2])            #> [1] "\"id\",\"city\",\"temp\"" "1,\"Yerevan\",31.5"

rds <- tempfile(fileext = ".rds")
saveRDS(df, rds)
print(identical(readRDS(rds), df))    #> [1] TRUE

Remember

  • read.csv + na.strings/colClasses for control over parsing
  • write.csv(..., row.names = FALSE) avoids an extra column
  • saveRDS/readRDS round-trip any R object exactly
  • Always check str() right after importing

Watch out for

  • A single odd value such as "n/a" turning a whole numeric column into character.

Check yourself

  1. A numeric column is read as character. What is the most likely cause?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: cran.r-project.org/doc/manuals/r-release/R-intro.html#Reading-data-from-files

Builds on

Unlocks

Nothing builds on this yet.