R Tutorial
Learn R programming step by step
Variables, loops, vectors, functions and importing data — explained with simple examples you can run in RStudio.
In this tutorial, you will learn how to import Excel spreadsheets into R and RStudio using the readxl package. We will cover both .xlsx and older .xls files, choosing a worksheet, importing selected rows and columns, handling column names and missing values, and checking the data after import.
The main function used in this tutorial is read_excel(). Unlike CSV files, Excel workbooks can contain multiple worksheets, so you can specify which sheet you want to read.
1. Read an Excel file from your current working directory.
2. Read a file by providing its complete path.
3. Use file.choose() to select a workbook through a file dialog.
4. Choose a worksheet by its name or position.
5. Import only a specific range or a limited number of rows.
If you want to read a csv file in R for data analysis and visualization or want to learn R Data Science as an absolute beginner, you may check our R Data Science from scratch to expert level R and Data Science for Beginners Course for fast learning with 1 on 1 Expert R Trainer and we Guarantee R learning with Real Life Projects.Microsoft Excel workbooks store tabular data in worksheets. The common extensions are .xlsx for modern Excel workbooks and .xls for older Excel files. A workbook may contain one worksheet or several worksheets, each with its own rows and columns.
R does not need Microsoft Excel installed to read these files. The readxl package reads Excel workbooks directly and imports the selected worksheet as a tibble, which is a modern form of data frame.
The read_excel() function is provided by the readxl package. Install the package once on your computer, then load it in each R session in which you want to use it.
install.packages("readxl")
After installation, load the package:
library(readxl)
You only need to run install.packages() the first time, or when you want to update the package. You should run library(readxl) in a new R session before using the package's functions, unless you call the function with its namespace, such as readxl::read_excel().
For practice, create an Excel workbook named employees.xlsx. In its first worksheet, enter the following data and save the workbook in a folder called excelfiles.
| Name | Age | Department | Salary |
|---|---|---|---|
| Peter | 35 | Sales | 3000 |
| John | 25 | IT | 4000 |
| Sarah | 29 | Finance | 2900 |
| David | 54 | IT | 7000 |
You can create this workbook in Microsoft Excel or another spreadsheet application that can save files in Excel format. Make sure the first row contains the column names.
The working directory is the folder R uses as the default location for relative file paths. Use getwd() to see the current working directory:
getwd()
If you want to use C:/excelfiles as the working directory on Windows, run:
setwd("C:/excelfiles")
Check the directory again if needed:
getwd()
Now read the workbook:
employees <- read_excel("employees.xlsx")
The object employees now contains the imported worksheet. If your workbook is in a different folder, either change the working directory or provide the complete file path, as shown below.
You do not need to change the working directory to read a file. You can supply the full path to the workbook directly. On Windows, forward slashes are convenient in R paths:
employees <- read_excel("C:/excelfiles/employees.xlsx")
You can also store the path in a variable and use that variable when importing the file:
file_path <- "C:/excelfiles/employees.xlsx"
employees <- read_excel(file_path)
Replace the example path with the actual location and file name on your computer. On macOS or Linux, use the path appropriate for your system.
If you do not know the exact path or file name, use file.choose(). It opens a file-selection dialog so you can select the workbook:
employees <- read_excel(file.choose())
This is often convenient for beginners using RStudio on a desktop computer. Select the Excel workbook when the dialog appears.
An Excel workbook can contain multiple worksheets. By default, read_excel() reads the first worksheet. To see the worksheet names before importing data, use excel_sheets():
excel_sheets("employees.xlsx")
Suppose the workbook contains worksheets named Employees, Departments and Summary. To read the worksheet named Departments, use:
departments <- read_excel("employees.xlsx", sheet = "Departments")
You can also choose a worksheet by its position. The following command reads the second worksheet:
second_sheet <- read_excel("employees.xlsx", sheet = 2)
Use the exact worksheet name, including spaces and punctuation. If the workbook only has one worksheet, you can omit the sheet argument.
Sometimes you only need part of a worksheet. The range argument lets you specify a cell range using Excel notation. For example, this reads cells A1 through D5 from the first worksheet:
employees_small <- read_excel("employees.xlsx", range = "A1:D5")
You can combine a range with a worksheet name:
data_part <- read_excel("employees.xlsx", sheet = "Employees", range = "A1:D10")
The range refers to Excel cell addresses. Make sure the selected range includes the header row if you want the first row to become the column names. A specified range is treated as the requested area even if some of its cells are blank.
If a worksheet starts with a title or explanatory notes above the actual table, use skip to skip rows before reading the data. For example, to skip the first two rows:
employees <- read_excel("employees.xlsx", skip = 2)
Use n_max when you only want to import a limited number of data rows:
first_100 <- read_excel("employees.xlsx", n_max = 100)
These arguments are useful for workbooks with introductory notes or large worksheets. Remember that skipping rows changes which row is treated as the header, unless you specify the range or header options appropriately.
By default, read_excel() uses the first row of the selected data as column names. If your worksheet has no header row, set col_names = FALSE:
data_no_headers <- read_excel("employees.xlsx", col_names = FALSE)
If the worksheet does not have a header row and you want to provide your own column names, pass a character vector to col_names:
employees <- read_excel("employees.xlsx", col_names = c("name", "age", "department", "salary"))
If the worksheet already has a header row but you want to replace those names, skip the existing header row and supply your own names:
employees <- read_excel("employees.xlsx", skip = 1, col_names = c("name", "age", "department", "salary"))
Blank cells are generally imported as missing values (NA). You can specify additional text values to treat as missing by using na. For example:
employees <- read_excel("employees.xlsx", na = c("", "N/A", "-"))
Only include values in na that should genuinely be treated as missing in your data. Otherwise, legitimate values could be converted to NA.
By default, read_excel() guesses each column's data type from the values in the worksheet. A column may be imported as text, numeric, logical or date-like data depending on its contents. Check the result with str().
If you know the intended types, you can provide col_types. For example, import the name and department as text and age and salary as numeric columns:
employees <- read_excel("employees.xlsx", col_types = c("text", "numeric", "text", "numeric"))
The number and order of entries in col_types must match the columns being imported. Use this option when automatic type guessing does not match your intended data types.
After importing the workbook, use these common functions to check whether the data has been read correctly.
1. View(employees): opens the data in RStudio's spreadsheet-style viewer.
2. head(employees): displays the first six rows.
3. dim(employees): returns the number of rows and columns.
4. nrow(employees): returns the number of rows.
5. ncol(employees): returns the number of columns.
6. colnames(employees): displays the column names.
7. str(employees): shows the structure and data types of the columns.
8. summary(employees): provides a quick summary of the imported variables.
Run the following commands in RStudio's Console, or place them in an R script and run them one at a time:
library(readxl)
employees <- read_excel("employees.xlsx")
View(employees)
head(employees)
dim(employees)
colnames(employees)
str(employees)
summary(employees)
If an Excel workbook is available online as a direct downloadable file, you can download it first and then pass the downloaded file path to read_excel(). For example:
library(readxl)
excel_url <- "https://example.com/data.xlsx"
download.file(excel_url, destfile = "downloaded_data.xlsx", mode = "wb")
web_data <- read_excel("downloaded_data.xlsx")
head(web_data)
Replace the example URL with a real direct URL to an Excel file. A webpage link that displays a file preview or requires sign-in is not necessarily a direct download link. The mode = "wb" argument is important on systems where downloaded files must be saved in binary mode.
The same read_excel() function can read both older .xls workbooks and modern .xlsx workbooks. You do not need to choose a separate function for each extension:
old_data <- read_excel("sales_2003.xls")
new_data <- read_excel("sales_2026.xlsx")
Make sure the file extension matches the actual file format and that the workbook is not damaged or password-protected in a way that prevents it from being read.
| Problem | Possible cause | What to try |
|---|---|---|
| File not found | The workbook is not in the working directory or the path is incorrect. | Check getwd() and use the complete file path. |
read_excel() not found | The readxl package has not been loaded. | Run install.packages("readxl") once, then library(readxl). |
| Wrong worksheet imported | The workbook has several worksheets and the first one is not the required sheet. | Check excel_sheets() and set sheet = "WorksheetName". |
| Column names look wrong | The actual header row is not the first row being read. | Check for title rows; adjust skip, range or col_names. |
| Numbers imported as text | The column contains mixed values, text markers or inconsistent cell formatting. | Inspect with str() and clean or convert the column after import. |
Some values appear as NA | Cells may be blank or contain values interpreted as missing. | Check the source worksheet and review the na argument. |
| Cannot read the downloaded file | The URL may point to an HTML page rather than the actual workbook. | Use a direct download URL and check that the downloaded file is a valid Excel workbook. |
| Argument | Purpose | Example |
|---|---|---|
path | Workbook path or file name. | read_excel("employees.xlsx") |
sheet | Worksheet name or position. | sheet = 2 |
range | Cell range to import. | range = "A1:D10" |
col_names | Use the first row as headers or supply names. | col_names = FALSE |
skip | Number of rows to skip before reading. | skip = 2 |
n_max | Maximum number of data rows to read. | n_max = 100 |
col_types | Specify the intended type for each column. | col_types = c("text", "numeric") |
na | Values to interpret as missing. | na = c("", "N/A") |
Create a workbook named sales.xlsx with a worksheet containing columns Product, Region and Sales. Then complete these tasks:
1. Import the workbook into an object named sales.
2. Display the first six rows.
3. Check the number of rows and columns.
4. Print the column names and data types.
5. If the workbook has a second worksheet, import that worksheet separately.
Example solution:
library(readxl)
sales <- read_excel("sales.xlsx")
head(sales)
dim(sales)
colnames(sales)
str(sales)
# Run this only if the workbook has a second worksheet:
sales_second_sheet <- read_excel("sales.xlsx", sheet = 2)
The readxl package provides a straightforward way to import Excel workbooks into R and RStudio. Use read_excel() for both XLS and XLSX files, select worksheets with sheet, and use range, skip or n_max when you only need part of a worksheet. After importing data, check it with View(), head(), dim(), colnames(), str() and summary().
Once your Excel data is in R, you can clean, summarize and visualize it using packages such as dplyr and ggplot2. Continue with the other R tutorials on Learn R to learn more about data manipulation and analysis.
Thesis & project help
Stuck on your own data?
Send us your dataset. An R expert will clean, analyse and visualise it — with commented code.
Get a Free Quote →Reply within 24 hours
Popular tutorials
Learning tracks