Working with data provided by R packages is a great way to learn the tools of data science, but at some point you want to stop learning and start working with your own data. In this chapter, you’ll learn how to read plain-text rectangular files into R. Here, we’ll only scratch the surface of data import, but many of the principles will translate to other forms of data. We’ll finish with a few pointers to packages that are useful for other types of data.
In this chapter, you’ll learn how to load flat files in R with the readr package, which is part of the core tidyverse.
Most of readr’s functions are concerned with turning flat files into data frames:
read_csv() reads comma-delimited files,
semicolon-separated files (common in countries where
, is used as the
read_tsv() reads tab-delimited files, and
read_delim() reads in files with any delimiter.
read_fwf() reads fixed-width files. You can specify fields either by
their widths with
fwf_widths() or their position with
read_table() reads a common variation of fixed-width files where columns are separated by white space.
read_log() reads Apache style log files. (But also check out
webreadr, which is built on top of
read_log() and provides many more helpful tools.)
These functions all have similar syntax: once you’ve mastered one, you can use the others with ease. For the rest ...