Front
Back
How do you read a CSV with readr::read_csv() and control types?
Use read_csv(file, col_types=) to enforce types and na strings.
Code:
library(readr)
df <- read_csv(‘data.csv’, col_types = cols(
id = col_integer(),
date = col_date(),
value = col_double()
), na = c(‘’, ‘NA’, ‘NULL’))
How do you read tab-delimited or arbitrary-delimited files?
Use read_tsv() or read_delim(delim = ‘;’).
Code:
library(readr)
read_tsv(‘data.tsv’)
read_delim(‘euro.csv’, delim=’;’)
How do you import Excel files?
Use readxl::read_excel() (no Java needed).
Code:
library(readxl)
df <- read_excel(‘workbook.xlsx’, sheet = ‘Sheet1’, range = NULL)
How do you save and load R objects efficiently?
Use saveRDS()/readRDS() for single objects.
Code:
saveRDS(model, file = ‘model.rds’)
model <- readRDS(‘model.rds’)
How do you write CSV output with readr?
Use write_csv() for UTF-8, fast writing.
Code:
library(readr)
write_csv(df, ‘clean_data.csv’, na = ‘’)
How do you skim a file before full import?
Use readr::read_lines() or vroom::vroom() for quick previews.
Code:
library(readr)
read_lines(‘data.csv’, n_max = 5)