• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / R / R Tips / 7 ways to read files with readr package in R

7 ways to read files with readr package in R

February 17, 2018 by cmdlinetips

read zipped file with readr
How to Read CSV Text File in R with readr?

There are multiple ways read text files in rectangular, like csv file, tsv file or text file with common delimitters. readr package, part of tidyverse, offers seven functions to load flat text files easily.

How to load a text file with readr package?

  1. read_csv(): to read comma delimited files
  2. read_csv2(): to read semicolon separated files
  3. read_tsv(): to read tab delimited files,
  4. read_delim(): to read in files with any delimiter.
  5. read_fwf(): to read fixed width files.
  6. read_table(): to read a common variation of fixed width files where columns are separated by white space.
  7. read_log(): to read Apache style log files.

It does look daunting at first to see these many options :-), Fear not, all of these functions to read text file have similar syntax. So, if you learn one, you have learned them all. Let us see examples of using read_csv().

How to load a csv file with read_csv()?

The most simple way to read a csv file is use read_csv with the csv file as argument. readr takes care of everything and will tell you how it parses the csv file by telling you the columns and their types.

>gapminder = read_csv("data/gapminder.csv")
Parsed with column specification:
cols(
  country = col_character(),
  year = col_integer(),
  pop = col_double(),
  continent = col_character(),
  lifeExp = col_double(),
  gdpPercap = col_double()
)

All these functions to read text loads of useful options. For example, here are all the options available for read_csv().

>help(read.csv)
read_csv(file, col_names = TRUE, col_types = NULL,
locale = default_locale(), na = c("", "NA"), quoted_na = TRUE,
quote = "\"", comment = "", trim_ws = TRUE, skip = 0, n_max = Inf,
guess_max = min(1000, n_max), progress = show_progress())
...

By default, read_csv() assumes the first line is column names and we can change that with

read_csv(file, col_names=FALSE)

How to Skip Lines while reading a CSV file in R?

Often the text files have meta data in the initial few lines. We can skip the meta data, by specifyng the number of lines we want to skip by using “skip = 0”. For example, to skip 4 lines while reading the csv file we can use

read_csv(file, skip = 4)

How to Skip Comment Lines while reading a CSV file in R?

Also, we can specify which lines are comment and not read them by specifying “comment” argument. For example, if the text file has comment with “#”, then we can read it as

read_csv(file, comment = "#")

How to Read Compressed Text/CSV files in R?

Another best thing about the readr package’s read text file functions is that, they all can recognize if the text file is compressed as gz, bz2, xz, or zip will be automatically uncompressed. You don’t have to do anything. Isn’t that cool?. read_csv() (and others) can automatically unzip it for you and load the unzipped file in R.

read_csv(file.gz)

And guess what, any file stored on the web with the link starting with http://, https://, ftp://, or ftps:// will be automatically downloaded.

How To Trim Leading and Trailing Whitespace in loading csv file?

One of the biggest challenges in data wrangling in data science is that leading and trailing white spaces present in the data. readr package’s read_csv() and other functions have option “trim_ws”, when set to TRUE all the leading and trailing whitespace around each field will be trimmed.

read_csv(file, trim_ws=TRUE)

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default Thumbnail3 Ways to Read a File and Skip Initial Comments in Python Default Thumbnail3 Ways to Read A Text File Line by Line in Python Default ThumbnailHow To Read a CSV File as Pandas Data Frame? How to read a numerical data/file in Python with numpy?

Filed Under: R Tips, Read CSV File in R, readr package, tidyverse Tagged With: How to Read CSV file in R, Read CSV File in R, read files with readr package

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version