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:

install.packages("xlsx")

Verify and Load the “xlsx” Package

Following command is used to verify and load the package:

# Verify the package is installed.
any(grepl("xlsx",installed.packages()))
# Load the library into R workspace.
library("xlsx")

Output:

TRUE
package ‘rJava’ successfully unpacked and MD5 sums checked
package ‘xlsxjars’ successfully unpacked and MD5 sums checked
package ‘xlsx’ successfully unpacked and MD5 sums checked

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:

  Roll_no   name Age addmission_date    dept
       1 Nikita  24      2018-01-01      IT
       2   Deep  29      2015-09-23      CS
       3  Aryan  21      2017-11-15 Finance
       4  Nidhi  25      2015-03-27      HR
       5   Aman  27      2013-05-21      IT

And sheet2:

    name      city
Nikita    Bangalore
   Deep    Delhi
  Aryan    Bhopal
  Nidhi    Raipur
   Aman    Noida

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:

# using sheet name
read.xlsx("Student.xlsx", sheetName = "Sheet1")
# using sheet index
read.xlsx("Student.xlsx", sheetIndex = 1)

Output:

  Roll_no   name Age addmission_date    dept
1       1 Nikita  24      2018-01-01      IT
2       2   Deep  29      2015-09-23      CS
3       3  Aryan  21      2017-11-15 Finance
4       4  Nidhi  25      2015-03-27      HR
5       5   Aman  27      2013-05-21      IT
  Roll_no   name Age addmission_date    dept
1       1 Nikita  24      2018-01-01      IT
2       2   Deep  29      2015-09-23      CS
3       3  Aryan  21      2017-11-15 Finance
4       4  Nidhi  25      2015-03-27      HR
5       5   Aman  27      2013-05-21      IT

Read the second worksheet:

# using sheet index
read.xlsx("Student.xlsx", sheetIndex = 2)

Output:

   name      city
1 Nikita Bangalore
2   Deep     Delhi
3  Aryan    Bhopal
4  Nidhi    Raipur
5   Aman     Noida

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

Example:

# reading data below the second line
read.xlsx("Student.xlsx", sheetName = "Sheet2", startRow = 3)

Output:

  Deep  Delhi
1 Aryan Bhopal
2 Nidhi Raipur
3  Aman  Noida

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

Example:

# read in a range of rows
read.xlsx("Student.xlsx", sheetName = "Sheet2", rowIndex = 3:5)

Output:

   Deep  Delhi
1 Aryan Bhopal
2 Nidhi Raipur
Reference: https://www.datacamp.com/community/tutorials/r-tutorial-read-excel-into-r

Related Topics

R Variables, Constants and Reserved Words

R Variables Variables are very similar to an open box, where you can put any value of your wish. We can change the values as well. Variables are used to store...

4 minutes read.

R JSON File

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. JSON file is often used when data is sent from a server to a...

2 minutes read.

R Tutorial

Introduction to R Programming R is nothing but a programming language, also a free software environment. This software used for predictive analysis, statistical analysis, graphical representation, data modeling, and reporting. R...

9 minutes read.

Analysis of Covariance in R Programming

Analysis of Covariance can also be named ANCOVA. We know that we use the concept of regression analysis for creating models which can explain the effect of the variation in...

2 minutes read.

Introduction to R Programming

What is R Programming? R is a programming language which provides an environment for software used for statistical analysis, graphics representation and reporting. It possesses an extensive catalog of...

4 minutes read.

R Functions

In programming, a function is a group of instructions that you want to use repeatedly, because of their complexity, are better self-contained in sub-program and called when needed. Hence, we can...

4 minutes read.

Logistic Regression in R Programming

Logistic Regression is a classification supervised machine learning algorithm in R programming. Logistic Regression can also be termed Binomial Logistic Regression, Binary Logistic Regression, or Logit Model. Logistic Regression is...

4 minutes read.

Decision Tree in R Programming

The decision tree uses the branching method to show every possible output for the specific input. We can draw a decision tree by hand or create it using specialized software...

2 minutes read.

R Operators

A symbol that tells the compiler to perform specific mathematical or logical operations is called operator. R language supports mainly 5 different types of operators, which are listed below: Arithmetic...

3 minutes read.

R Inheritance

One the main concept of object oriented programming is inheritance which allows us to define a new class of existing classes. This means that we can derive new classes from...

5 minutes read.

Binomial Distribution in R Programming

In this article, we will talk about the Binomial distribution in R programming. The binomial distribution is a type of probability distribution. As it is a discrete distribution, it will...

3 minutes read.

R Pie Charts

R programming language provides many numbers of libraries to create charts and graphs. A pie-chart is a representation of data as slices of a circle with different colors representing counts...

2 minutes read.

First R Program

First  Hello World R Program As a convention, our first R program will be the “Hello World!” program. We can run our R program either at R command prompt or we...

3 minutes read.

How to make Boxplots in R

R Boxplots Boxplot is a measure of how well the data is distributed in a data set. It is used to give a summary of one or several numeric variables. The...

3 minutes read.

Poisson Distribution in R Programming

Poisson distribution is a type of distribution that deals with the probability distribution of the data values by taking the mean into consideration. Poisson distribution will estimate the probability value for...

3 minutes read.

Normal Distribution in R Programming

In statistics, the normal distribution is a type of probability function. Normal distribution tells the user about the distribution of data values in the dataset. It is a very important...

3 minutes read.

R Basic Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

4 minutes read.

Pie Charts in R Programming

R programming has various libraries to plot various types of charts and graphs. A pie chart is a circular statistical chart or graph, which is divided into slices to represent the numerical...

4 minutes read.

R If Else Statement

If statement It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if...

2 minutes read.

R Switch Statement

It is an alternate way to a nested if…else statement. A switch statement permits an expression to be tested against a list of case values. In short, we can say...

1 minute read.