Reading Data into R


This entry is part 1 of 1 in the series R Data Import
  • Reading Data into R

Here is an online resource book called R for Data Science. Have a look at chapter 11 Data Import.

We’ll use the readr() function which is part of the core tidyverse package. You’ve probably already installed tidyverse, so all you’ll need to do for your current session is to load it with library(tidyverse). Most of readr’s functions are concerned with turning flat files into data frames. We have read_csv(), read_fwf() and read_log(). csv is comma-separated values. fwf is fixed-width files and log is log files.

here Package

It’s a good idea to load the here package before you save any of your files.

Reading CSV Files

You can use read_csv() to read CSV files into RStudio.

Reading Excel Files

To import spreadsheet data into R, you can use the readxl package. Readxl supports both the legacy .xls file format and the modern xml-based .xlsx file format. The readxl package is part of the tidyverse but is not a core tidyverse package, so you need to load readxl in R by using the library() function. library(readxl).

You can use the read_excel() function to read a spreadsheet file just like you used read_csv() function to read a .csv file. The code for reading the example file “type-me.xlsx” includes the path to the file in the parentheses of the function.

For more information on readxl, have a look at the tidyverse website under readxl.

Leave a Reply