0% found this document useful (0 votes)
26 views4 pages

How To Import An Excel File Into R

notes

Uploaded by

bala vinothini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

How To Import An Excel File Into R

notes

Uploaded by

bala vinothini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

How to Import an Excel File into

R (example included)
To import an Excel file into R using the readxl package:

library("readxl")

df = read_excel("Path where your Excel file is stored\\File Name.xlsx")

print(df)

To import a specific sheet within the Excel file:

library("readxl")

df = read_excel("Path where your Excel file is stored\\File Name.xlsx", sheet="Your sheet


name")

print(df)
steps to Import an Excel file Into R

Step 1: Install the readxl package


In the R Console, type the following command
to install the readxl package:

Copy
install.packages("readxl")

Follow the instructions to complete the installation.

You may want to check the following guide that explains how
to install a package in R.

Step 2: Prepare your Excel File


Product Price
Let’s suppose
that you have an
Excel file Refrigerator 1200 with
some data
about products:
Oven 750
And let’s say that
the Excel file name
is produ Dishwasher 900 ct_list, a
nd your goal is to
import that file
into R. Coffee Maker 300

Step 3:
Import the Excel file into R
In order to import your file, you’ll need to use the following template
in the R Editor:

Copy
library("readxl")

df = read_excel("Path where your Excel file is stored\\File


Name.xlsx")

print(df)

For demonstration purposes, let’s assume that an Excel file is stored


under the following path:

C:\\Users\\Ron\\Desktop\\Test\\product_list.xlsx

Where:

 product_list is the actual file name; and


 .xlsx is the Excel file extension. For previous versions of Excel,
use the file extension of .xls
Note that a double backslash (‘\\’) was used within the path name.
By adding a double backslash, you’ll avoid the following error in R:

Error: ‘\U’ used without hex digits in character string


starting “”C:\U”

Here is the complete code to import the Excel file for our example:

Copy
library("readxl")

df = read_excel("C:\\Users\\Ron\\Desktop\\Test\\
product_list.xlsx")

print(df)

You’ll need to adjust the path to reflect the location where the Excel
file is stored on your computer.

Once you run the code in R, you’ll get the same values as in the
Excel file:

Product Price

1 Refrigerator 1200

2 Oven 750

3 Dishwasher 900

4 Coffee Maker 300


Here are the steps to import an Excel file into R using the readxl package:
1. Install the readxl package using the command install.packages("readxl")
2. Import the package using the command library(readxl)
3. Upload the Excel file using the command data <- read_excel("D:/path/file.xlsx")
You can also import an Excel file into R by:
 Going to File > Import Dataset > From Excel in RStudio
 Browsing for the Excel file
 Selecting Import at the bottom right of the window
The readxl package can read the first worksheet, other workbooks, specific
rows, specific cells, and data with no header row.

You might also like