Combine Multiple Excel Worksheets into Single Dataframe in R
Last Updated :
17 Jun, 2021
In this article, we will discuss how to combine multiple excel worksheets into a single dataframe in R Programming Language.
The below XLSX file "gfg.xlsx" has been used for all the different approaches.


Method 1: Using readxl package
The inbuilt setwd() method is used to set the working directory in R.
The readxl package in R is used to import and read Excel workbooks in R, which can be used to easily work and modify the .xslsx sheets. It can be installed and loaded into the R working space using the following syntax :
install.packages("readxl")
Initially, the excel_sheets() method is invoked to fetch all the worksheet names contained in the Excel workbook, with the specified file path.
excel_sheets(path)
The lapply() method in R is used to apply a function (either user-defined or pre-defined) to a set of components contained within an R list or dataframe. The lapply( ) method returns an object of the same length as that of the input object.
Syntax: lapply( obj , FUN)
Arguments:
obj - The object to apply the function on
FUN - The function to be applied over different components of the object obj.
The FUN is the read_excel method of this package store, which is used to read the contents of the specified sheet name into a tibble, which is a tabular-like structure used to store data in fixed rows and columns. The lapply method applies the read_excel method over every sheet of the workbook.
Syntax: read_excel(path, sheet)
Arguments:
path - The file path
sheet - The sheet name to read
The final tibble is produced by the inbuilt method bind_rows(), which takes as input the individual dataframes and assigns them a singular ID attribute key. The output of bind_rows() will contain a column if that column appears in any of the inputs. The output tibble contains the records contained in all the sheets , along with their respective data types. The column name "Sheet" is used to lead all the rows by forming a primary column.
R
# installing the required libraries
library(readxl)
library(tidyverse)
# specifying the path for file
path <- "/Users/mallikagupta/Desktop/"
# set the working directory
setwd(path)
# accessing all the sheets
sheet = excel_sheets("Gfg.xlsx")
# applying sheet names to dataframe names
data_frame = lapply(setNames(sheet, sheet),
function(x) read_excel("Gfg.xlsx", sheet=x))
# attaching all dataframes together
data_frame = bind_rows(data_frame, .id="Sheet")
# printing data of all sheets
print (data_frame)
Output
# A tibble: 11 x 4
Sheet ID Name Job
<chr> <dbl> <chr> <chr>
1 Sheet1 1 A Engineer
2 Sheet1 2 B CA
3 Sheet1 3 C SDE
4 Sheet1 4 D CA
5 Sheet1 5 E SDE
6 Sheet2 6 F SDE
7 Sheet2 7 G CA
8 Sheet2 8 H CA
9 Sheet2 9 I Admin
10 Sheet3 10 J SDE
11 Sheet3 11 K Admin
Method 2: Using rio package
The rio package is used to stimulate quick and easy data import and export operations to be performed in R. Rio makes deductions about the file format itself which can be used to read files easily.
install.packages("rio")
The import() and export() methods in R determine the data structure of the specified file extension. The method import_list() imports a list of dataframes from a multi-object file, for instance, an Excel workbook or an R zipped file.
Syntax: import_list(file, rbind = FALSE)
Arguments :
file - The file name of the Excel workbook to access
rbind - Indicator of whether to combine or not the dataframes into a single dataframe
R
# specifying the path name
path <- "/Users/mallikagupta/Desktop/Gfg.xlsx"
# importing the required library
library(rio)
# reading data from all sheets
data <- import_list(path , rbind=TRUE)
# print data
print (data)
Output
ID Name Job _file
1 1 A Engineer 1
2 2 B CA 1
3 3 C SDE 1
4 4 D CA 1
5 5 E SDE 1
6 6 F SDE 2
7 7 G CA 2
8 8 H CA 2
9 9 I Admin 2
10 10 J SDE 3
11 11 K Admin 3
Similar Reads
Export Dataframes to Multiple Excel Sheets in R
An excel workbook is used to contain tabular information stored within a cell-like structure. Every excel workbook in R contains multiple sheets to contain the data belonging to the same domain information. The excel sheet can contain columns belonging to different data types. The Excel files can be
5 min read
How to Combine Data from Multiple Sheets in one File?
Merging data into a single sheet is a crucial and significant skill to know. For example, you have the monthly sales of a company in the 12 worksheets of excel. Now you want to calculate the net revenue made in the entire year. So, we need to merge all the data in the 12 excel sheets, this task coul
4 min read
How to Export Multiple Dataframes to Different Excel Worksheets in R
In this article, we are going to see how to export multiple dataframe to different Excel Worksheets in R Programming Language. We will be using the xlsx module. The xlsx library provides R functions to read/write/format Excel files and their formats. The xlsx package gives programmatic control of Ex
4 min read
How to Create Multiple Sheets in Excel workbook Using R
In this article, we will discuss how to create multiple sheets in an excel file using the xlsx package. As we all know in general an excel file might contain one or more than one sheet present in it. Manually we can create and insert data into multiple sheets in Excel GUI Application but when it co
2 min read
Import multiple excel sheets into in R
In this article, we are going to see how to import multiple Excel sheets into the R language. Excel provides us with multiple worksheets. For example, in the below Excel workbook StudentData, we have two worksheets - sheet 1 is Student Details and sheet 2 is Subject Details. For importing multiple
2 min read
How to Combine Two Columns into One in R dataframe?
In this article, we will discuss how to combine two columns into one in dataframe in R Programming Language. Method 1 : Using paste() function This function is used to join the two columns in the dataframe with a separator. Syntax: paste(data$column1, data$column2, sep=" ") where data is the input
2 min read
How to Merge multiple CSV Files into a single Pandas dataframe ?
While working with CSV files during data analysis, we often have to deal with large datasets. Sometimes, it might be possible that a single CSV file doesn't consist of all the data that you need. In such cases, there's a need to merge these files into a single data frame. Luckily, the Pandas library
3 min read
How to Write Pandas DataFrames to Multiple Excel Sheets?
In this article, we will see how to export different DataFrames to different excel sheets using python. Pandas provide a function called xlsxwriter for this purpose. ExcelWriter() is a class that allows you to write DataFrame objects into Microsoft Excel sheets. Text, numbers, strings, and formulas
6 min read
Combine two Pandas series into a DataFrame
In this post, we will learn how to combine two series into a DataFrame? Before starting let's see what a series is?Pandas Series is a one-dimensional labeled array capable of holding any data type. In other terms, Pandas Series is nothing but a column in an excel sheet. There are several ways to con
3 min read
How to convert excel content into DataFrame in R ?
R Programming Language allows us to read and write data into various files like CSV, Excel, XML, etc. In this article, we are going to discuss how to convert excel content into DataFrame in R Programming. To read an excel file itself, read.xlsx() function from xlsx is used. Installation This module
2 min read