In this Article, Let us look at how to read or import an Excel file in the R language.
excelFile.xlsx:
Reading an Excel File in R
Below are the ways to read an excel file in R:
Method #1: Using read_excel() Function of readxl package
read_excel() Function:
The read_excel() method is used to read an Excel file and is only accessible after the readxl library has been imported into the R program.
Installing read_excel:
Syntax:
install.packages("readxl")
Output:
Installing package into ‘C:/Users/cirus/AppData/Local/R/win-library/4.2’ (as ‘lib’ is unspecified) --- Please select a CRAN mirror for use in this session --- trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.2/readxl_1.4.0.zip' Content type 'application/zip' length 1197616 bytes (1.1 MB) downloaded 1.1 MB package ‘readxl’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\cirus\AppData\Local\Temp\Rtmpo5UGo0\downloaded_packages
Syntax:
read_excel(filepath)
Parameters
filepath: It is the input file to be read or imported
Example
Approach:
- Import the readxl package using the library() function.
- Pass the file path to be read as an argument to the read_excel() function and store it in a variable.
- Here it reads/imports the given excel file.
- Display the data of the above given excel file.
- The Exit of the Program.
Below is the implementation:
# Import the readxl package using the library() function library(readxl) # Pass the file path to be read as an argument to the read_excel() function # and store it in a variable. # Here it reads/imports the given excel file gvn_excelfile <- read_excel("C:/Users/cirus/Downloads/excelFile.xlsx") # Display the data of the above given excel file gvn_excelfile
Output:
# A tibble: 12 × 10 `Website name` Name Age City State Student Employed Married <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <chr> 1 Sheets Tips Vikram 21 Hyderabad Tela… Yes No No 2 Python-Programs Ashritha 22 Pune Mumb… Yes No No 3 BTechGeeks Manish 19 Indore Madh… Yes No No 4 PythonArray Anish 20 Rajkot Guja… Yes No No 5 Sheets Tips Vishal 18 Ranchi Jhar… Yes No No 6 Python-Programs Pavan 23 Lucknow Utta… No Yes No 7 Sheets Tips John 32 Los Angeles Amer… No Yes Yes 8 Python-Programs Virat 34 Agra Delhi No Yes No 9 BTechGeeks Cary 36 Tokyo Japan No Yes Yes 10 PythonArray Tara 39 Helsinki Finl… No Yes Yes 11 Sheets Tips Smith 42 Nairobi Kenya No Yes Yes 12 Python-Programs Elizabeth 66 Paris Italy No Yes Yes # … with 2 more variables: Scholorship <chr>, Id <dbl>
Method #2: Using read.xlsx() Function of xlsx package
xlsx package:
The excel package is a robust java-based language for reading, writing, and updating data from and to excel files.
read.xlsx() Function:
The read.xlsx() function is imported from the R language’s xlsx package and used to read/import an excel file.
Syntax:
read.xlsx(filepath)
Parameters
filepath: It is the input file to be read or imported
Installation of xlsx package
Before we work with this package we must first install it. Type the below command in the R console:
install.packages("xlsx")
Output:
Installing package into ‘C:/Users/vicky/AppData/Local/R/win-library/4.2’ (as ‘lib’ is unspecified) also installing the dependencies ‘rJava’, ‘xlsxjars’ trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.2/rJava_1.0-6.zip' Content type 'application/zip' length 1245703 bytes (1.2 MB) downloaded 1.2 MB trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.2/xlsxjars_0.6.1.zip' Content type 'application/zip' length 9485708 bytes (9.0 MB) downloaded 9.0 MB trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.2/xlsx_0.6.5.zip' Content type 'application/zip' length 375143 bytes (366 KB) downloaded 366 KB package ‘rJava’ successfully unpacked and MD5 sums checked package ‘xlsxjars’ successfully unpacked and MD5 sums checked package ‘xlsx’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\vicky\AppData\Local\Temp\Rtmp84RZij\downloaded_packages
Example
Approach:
- Import the xlsx package using the library() function
- Pass the file path to be read as an argument to the read.xlsx() function and store it in a variable
- Here it reads/imports the given excel file.
- Display the data of the above given excel file.
- The Exit of the Program.
Below is the implementation:
# Import the xlsx package using the library() function library(xlsx) # Pass the file path to be read as an argument to the read.xlsx() function # and store it in a variable # Here it reads/imports the given excel file. gvn_excelfile <-read.xlsx("C:/Users/cirus/Downloads/excelFile.xlsx") # Display the data of the above given excel file gvn_excelfile
Output:
# A tibble: 12 × 10 `Website name` Name Age City State Student Employed Married <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <chr> 1 Sheets Tips Vikram 21 Hyderabad Tela… Yes No No 2 Python-Programs Ashritha 22 Pune Mumb… Yes No No 3 BTechGeeks Manish 19 Indore Madh… Yes No No 4 PythonArray Anish 20 Rajkot Guja… Yes No No 5 Sheets Tips Vishal 18 Ranchi Jhar… Yes No No 6 Python-Programs Pavan 23 Lucknow Utta… No Yes No 7 Sheets Tips John 32 Los Angeles Amer… No Yes Yes 8 Python-Programs Virat 34 Agra Delhi No Yes No 9 BTechGeeks Cary 36 Tokyo Japan No Yes Yes 10 PythonArray Tara 39 Helsinki Finl… No Yes Yes 11 Sheets Tips Smith 42 Nairobi Kenya No Yes Yes 12 Python-Programs Elizabeth 66 Paris Italy No Yes Yes # … with 2 more variables: Scholorship <chr>, Id <dbl>