6.2 C
London
Thursday, December 19, 2024
HomeRImport & Export Data in RHow to Import Excel Files into R (Step-by-Step)

How to Import Excel Files into R (Step-by-Step)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The easiest way to import an Excel file into R is by using the read_excel() function from the readxl package.

This function uses the following syntax:

read_excel(path, sheet = NULL)

where:

  • path: Path to the xls/xlsx file
  • sheet: The sheet to read. This can be the name of the sheet or the position of the sheet. If this is not specified, the first sheet is read.

This tutorial provides an example of how to use this function to import an Excel file into R.

Example: Import an Excel File into R

Suppose I have an Excel file saved in the following location:

C:UsersBobDesktopdata.xlsx

The file contains the following data:

Import Excel into R

The following code shows how to import this Excel file into R:

#install and load readxl package
install.packages('readxl')
library(readxl)

#import Excel file into R
data C:\Users\Bob\Desktop\data.xlsx')

Note that we used double backslashes (\) in the file path to avoid the following common error:

Error: 'U' used without hex digits in character string starting ""C:U"

We can use the following code to quickly view the data:

#view entire dataset
data

#A tibble: 5 x 3
 team  points  assists
       
1 A         78      12
2 B         85      20
3 C         93      23
4 D         90       8
5 E         91      14

We can see that R imported the Excel file and automatically determined that team was a string variable while points and assists were numerical variables.

Additional Resources

The following tutorials explain how to import other file types into R:

How to Import CSV Files into R
How to Import SAS Files into R
How to Manually Enter Raw Data in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories