R readr
The readr package provides file I/O functions.
Contents
Installation
readr is commonly used through Tidyverse.
Usage
Data Format |
Ingress Example |
Outgress Example |
Native R |
read_rds("data.rds") |
write_rds(df, "data.rds") |
Comma-delimited |
read_csv("data.csv") |
write_csv(df, "data.csv") |
Tab-delimited |
read_tsv("data.tsv") |
write_tsv(df, "data.tsv") |
Any delimiter |
read_delim("data.txt", delim="|") |
write_delim(df, "data.txt", delim="|") |
Fixed width |
read_fwf("data.txt", fwf_widths(c(2,2,NA))) |
N/A |
These functions (aside from the native R ones) all take these arguments:
If col_names is a logical, it toggles whether the first row is read as a header.
If col_names is a vector, the variables are named accordingly.
skip causes the parser to skip some number of rows.
n_max causes the parser to stop after some number of rows.
guess_max is the number of rows to parse before determining the storage type. To parse all data before type casting, try guess_max = Inf.
col_select subsets the columns to store. It should be specified like col_select = c(1,2,3). Something like col_select = c(foo, bar, baz) can be used if there is header information.