R Excel Files

Microsoft Excel is the spreadsheet program which stores data in the .xls and .xlsx format. R has the facility to read directly from these files using some excel specific packages.

Some excel packages are: XLConnect, xlsx, gdata, RODBC, RExcel, etc. Here, we will use the xlsx package.

Install xlsx package

Xlsx package provides all necessary tools to interact with Excel files from R. Many of the benefits of the xlsx package come from being able to export and format excel files from R. Following command is used to install the xlsx package in R:

Verify and Load the “xlsx” Package

Following command is used to verify and load the package:

Output:

Creating an Excel File

Create two worksheets in excel. Use the default name sheet1 and sheet2. Give the name of excel is Student.xlsx.

Sheet1:

And sheet2:

Reading the Excel File

In R, read.xlsx() function is used to read the excel file. The result will store as a data frame in the R environment.

Example:

Let’s see an example to read the first worksheet using a sheet index or name:

Output:

Read the second worksheet:

Output:

If we want to read data starts further down in the Excel worksheet we can include the startRow argument.

Example:

Output:

We can also specify the number of rows and columns. For this xlsx package has rowIndex and colIndex argument.

Example:

Output:

Reference:
https://www.datacamp.com/community/tutorials/r-tutorial-read-excel-into-r