How to Import Excel Files into R

Here are two ways to import Excel files in R:

  1. Using read_excel()
  2. Using the built-in menu Options of Rstudio

Let’s say we have an Excel file like this, and we will import this file in R.

Using read_excel()

Method 1: Using read_excel()

The easiest method for reading Excel files in R is using the readxl package’s “read_excel()” function. You will need to install this package first if you haven’t already.

To install the readxl package:

install.packages("readxl")

After installing the package, you can use the following code to read an Excel file:

Syntax

read_excel(filename, sheet, dtype = “float32”)

Parameters

  1. filename:- File name to read from.
  2. sheet:- Name of the sheet in Excel file.
  3. dtype:- Numpy data type.

Return value

It is treated as a data frame.

After installing the package, you can use the following code to read an Excel file:

# Load the readxl package
library(readxl)

# Read an Excel file into a data frame
my_data <- read_excel("simple_data_frame.xlsx")

# Display the first few rows of the data
head(my_data)

Output

two ways to import Excel files in R

Method 2: Using the built-in menu Options of Rstudio

If you are using RStudio, you can also use the built-in menu options to import Excel files:

Step 1: Navigate to the Menu Bar

Go to the menu bar at the top and click on File -> Import Dataset -> From Excel.

Navigate to the Menu Bar

Step 2: Choose File

A window will pop up. Navigate to the Excel file you wish to import and select it.

Choose File

Step 3: Preview and Import

You’ll see a preview of the data. If it looks correct, click the Import button to import the data into R.

Using the built-in menu Options of Rstudio

Related posts

Export DataFrame to CSV in R

Read CSV File into DataFrame in R

sink() function in R

read.delim() function in R

Leave a Comment