DEV Community

Mahesh K
Mahesh K

Posted on • Updated on


R Language : Read Excel files In R

Let's discuss how to read excel files from R. Most of the office and the small business data is still being saved in spreadsheet. And Microsoft Excel is one of the popular spreadsheet software that is being used for storing this data.

These days even the Google docs are being used but most of the people still prefer the offline desktop based spreadsheet files like excel.

So here we are going to take a look at the simple way to read the excel files in R language. You can check out the video instruction of this tutorial - Read Excel files in R Language.

We are going to take a look at some of the steps like :

  1. Install package that reads excel file
  2. Read the excel file
  3. Enjoy

1. Install xlsx Package

XLSX package is one of the known package in the R repository. This package works on Windows, Mac, Linux and allows you to read and write the excel file. You can also add the images and charts with this library. You can install the xlsx package with the following command.

Enter fullscreen mode Exit fullscreen mode

2. Read the Excel File data

Now that we have downloaded the package it's time to test drive with the code. So here's simple snippet to read our excel file named mydb.xlsx. Here we are going to read the first sheet in the file.

fp <-  system.file("demo", "mydb.xlsx", package  =  "xlsx")  
res  <-  read.xlsx(fp, 1)  
head(res[, 1:6])
Enter fullscreen mode Exit fullscreen mode

Let's discuss the code from line 1.

In line 1, we are basically loading the xlsx library. In 2nd line we are loading the file in variable, with reference to package xlsx. In 3rd line we are reading the xlsx files first sheet. And finally we are display records starting from 1st row to 6th row and displaying it on the console.

That's it.

You can check the official package documentation on GitHub for further manipulation of the excel files. I hope that the information explained here helped you to work with excel file in a simple way.

Top comments (0)