Hands-On Exploratory Data Analysis with R
上QQ阅读APP看书,第一时间看更新

Reading in Excel data with the readxl R package

The readxl R package makes it very easy and straightforward to read data from Excel files into R:

We start by loading the module into R:

library(readxl)

Reading Excel data with readxl is simple; we just need to call the read_excel function and pass in the file path, as follows:

read_excel("data.xls")

readxl supports both the old XLS and the new XLSX format. It guesses the format from the extension of the file being read:

read_excel("data.xlsx")

Excel files sometimes have multiple sheets. For example, the following Excel file has multiple sheets:

You can easily handle them with the readxl package. To see the number of sheets in the Excel document, use the excel_sheets command:

excel_sheets("data.xlsx")
#1> [i] "sheet1" "sheet2"

We can also specify which sheet should be accessed by using read_excel with the sheet argument:

read_excel("data.xlsx", sheet= 1)

To access a particular sheet of Excel, the sheet parameter is used: 

read_excel("data.xlsx", sheet= "sheet1")