Section 10 Reading in data, revisited
R can read data from text files (this includes csv, tsv, txt, etc.) as well as from other statistical software like Excel, SAS, Stata etc.
10.1 Rectangular data
For reading in rectangular text data, readr
is your best bet. For a csv, your code would look like:
10.2 Data from other statistical software
The haven
package is best for reading in data from SAS, Stata, and SPSS. The readxl
package works for all excel files, including options for sheets and things like that.
10.3 GIS data
Packages like sf
and raster
work for importing/writing vector and raster data, respectively.
10.4 Writing data
As well as reading in a variety of different data formats, R can also write to these formats.
10.5 Cleaning data
Along with wrangling, there are a few packages that help with the data reading process. janitor is one of my favorites. I like the clean_names()
function a lot because it will automatically make your column names easily machine readable (no spaces, special characters, etc).