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.
This module is not built into R but it can be installed and imported explicitly. Open the R console and type the command given below to install xlsx package.
read_xlsx() can be used in the following way:
Syntax: read.xlsx(“Excel File Path”, sheetName = “sheet name”, …)
Parameters: it is necessary to give the Excel file path and sheetName as argument in read.xlsx() function but many other parameters can be used by this function such as colNames, rowNames, skipEmptyRows, skipEmptyCols, rows, cols, etc. for doing some extra modifications. Here we used colIndex as parameter for getting only column that we want. here we give 1 as a value of colIndex parameter for getting first column of Excel file.
File in use:
ID Name Age 1225 John 19 1226 Jane 19 1227 Bill 65 1228 Elon 49
First, we import xlsx package by using the library() function then give the full path of the Excel file to excel_path named variable. To create a dataframe keep extracting columns from the file and combine them into one data frame once done.