0
0
R Programmingprogramming~5 mins

Excel files with readxl in R Programming

Choose your learning style9 modes available
Introduction

We use readxl to open and read Excel files easily in R. It helps us get data from spreadsheets into R for analysis.

You have data saved in an Excel file and want to analyze it in R.
You want to read specific sheets from an Excel workbook.
You need to import data without installing heavy Excel software.
You want to quickly check or clean data stored in Excel.
You want to automate reading Excel files in your R scripts.
Syntax
R Programming
library(readxl)
data <- read_excel(path, sheet = NULL, range = NULL, col_names = TRUE)

path is the file location of the Excel file.

sheet lets you pick which sheet to read by name or number.

Examples
Reads the first sheet from the Excel file named data.xlsx.
R Programming
library(readxl)
data <- read_excel("data.xlsx")
Reads the sheet named Sales from the Excel file.
R Programming
data <- read_excel("data.xlsx", sheet = "Sales")
Reads the second sheet by position from the Excel file.
R Programming
data <- read_excel("data.xlsx", sheet = 2)
Reads only the cells from A1 to C10 in the first sheet.
R Programming
data <- read_excel("data.xlsx", range = "A1:C10")
Sample Program

This program loads the readxl package, reads the first sheet of example.xlsx, and prints the data to the console.

R Programming
library(readxl)

# Assume we have an Excel file named 'example.xlsx' with a sheet 'Sheet1'
# that has a small table of data.

# Read the whole first sheet
my_data <- read_excel("example.xlsx")

# Print the data
print(my_data)
OutputSuccess
Important Notes

You need to have the readxl package installed. Use install.packages("readxl") if needed.

readxl works well for .xls and .xlsx files without needing Excel installed.

It does not write Excel files, only reads them.

Summary

readxl helps you open Excel files in R easily.

You can choose sheets by name or number and read specific cell ranges.

It is simple and does not require Excel software on your computer.