Import Excel Data into an R Data Frame
Excel workbooks are a common source of tabular data. In R, you can import an .xlsx or .xls worksheet with the readxl package and store the result in an object for analysis.
The read_excel() function returns a tibble, which already behaves like a data frame for most R operations. Convert it with data.frame() or as.data.frame() only when a function specifically requires a base R data frame.
Reference: Read Excel File Data in R
Install readxl before importing Excel data into R
Install the readxl package once from CRAN, and then load it in each new R session.
install.packages("readxl")
library(readxl)
You can also call the import function with its package name, as in readxl::read_excel(), without running library(readxl).
Import an Excel worksheet into an R data frame
First, pass the Excel file path to read_excel(). The imported worksheet is returned as a tibble. Then pass that object to data.frame() to create a base R data frame.
> exceldata = read_excel("C:\\tutorialkart\\r\\sample.xlsx")
> dfdata = data.frame(exceldata)
> dfdata
ID Name Salary
1 22 John 25000
2 41 Samantha 30000
3 15 Ron 37000
4 63 Rick 15000
5 87 Gary 56000
In this example, exceldata contains the imported tibble, while dfdata contains the equivalent base R data frame.
A shorter version uses as.data.frame() directly around the Excel import call.
dfdata <- as.data.frame(
readxl::read_excel("C:/tutorialkart/r/sample.xlsx")
)
print(dfdata)
Forward slashes are convenient in Windows file paths because they do not need to be escaped. Escaped backslashes, such as C:\\folder\\file.xlsx, are also valid.
Use the imported Excel tibble without converting it
For most data analysis tasks, explicit conversion is unnecessary. Tibbles support common data-frame operations such as selecting columns, filtering rows, calculating summaries, and accessing values with column names.
employees <- readxl::read_excel("C:/tutorialkart/r/sample.xlsx")
employees$Name
employees[employees$Salary > 30000, ]
mean(employees$Salary, na.rm = TRUE)
Convert the tibble only when older R code, a package function, or an export workflow expects an object whose class is exactly data.frame.
Import a specific Excel sheet into an R data frame
Excel workbooks can contain several worksheets. Use excel_sheets() to inspect their names, and then select a worksheet with the sheet argument.
file_path <- "C:/tutorialkart/r/employees.xlsx"
readxl::excel_sheets(file_path)
employees_df <- as.data.frame(
readxl::read_excel(file_path, sheet = "Employees")
)
You may also select a worksheet by its one-based position. For example, sheet = 2 imports the second worksheet in the workbook.
Import selected Excel cells, rows, or columns
Use the range argument when the required table occupies a known cell area. Use skip when introductory text appears above the column headings, and use n_max to limit the number of imported data rows.
sales_df <- as.data.frame(
readxl::read_excel(
"C:/tutorialkart/r/report.xlsx",
sheet = "Sales",
range = "A2:D50"
)
)
preview_df <- as.data.frame(
readxl::read_excel(
"C:/tutorialkart/r/report.xlsx",
skip = 2,
n_max = 10
)
)
A range can also include the worksheet name, for example "Sales!A2:D50". When a strict range is supplied, only cells inside that range are imported.
Set Excel column names and data types during import
By default, read_excel() treats the first imported row as column names and guesses column types from the cell values. Use col_names, col_types, and na when the worksheet needs more controlled handling.
employees_df <- as.data.frame(
readxl::read_excel(
"C:/tutorialkart/r/sample.xlsx",
col_names = c("employee_id", "employee_name", "salary"),
col_types = c("numeric", "text", "numeric"),
na = c("", "NA", "Not available")
)
)
Explicit column types are useful when identifiers with leading zeros must remain text, numeric columns contain occasional text values, or dates are not being interpreted as expected.
Import Excel data into RStudio on Windows or macOS
The R code is the same in RStudio on Windows, macOS, and Linux. Only the file path differs. On macOS, an absolute path may look like /Users/name/Documents/sample.xlsx. On Windows, it may look like C:/Users/name/Documents/sample.xlsx.
# Windows
windows_df <- readxl::read_excel(
"C:/Users/name/Documents/sample.xlsx"
)
# macOS
mac_df <- readxl::read_excel(
"/Users/name/Documents/sample.xlsx"
)
When the workbook is stored in the current working directory, the file name alone is sufficient. Use getwd() to display the current directory and file.exists() to verify the path.
getwd()
file.exists("sample.xlsx")
exceldata <- readxl::read_excel("sample.xlsx")
Check the imported Excel data frame in R
After importing the worksheet, inspect its dimensions, column names, structure, and initial rows before beginning analysis.
dim(dfdata)
names(dfdata)
str(dfdata)
head(dfdata)
summary(dfdata)
These checks help identify incorrect headers, unexpected missing values, duplicate column names, and columns that were imported with the wrong type.
Fix common Excel import errors in R
R cannot find the read_excel function
An error such as could not find function "read_excel" means that readxl is not loaded or installed. Run install.packages("readxl") once, followed by library(readxl), or use readxl::read_excel().
R reports that the Excel file does not exist
Check spelling, the file extension, and the complete path. File paths copied from Windows Explorer may contain single backslashes, which must be replaced with forward slashes or doubled in an R string.
The wrong Excel row became the data-frame header
If the workbook contains a title or notes above the table, use skip or range. Set col_names = FALSE when the worksheet has no header row, or provide a character vector of replacement names.
Excel numbers or dates were imported as text
This can occur when a column contains mixed cell types. Clean inconsistent cells in the workbook where possible, increase guess_max, or specify the expected types with col_types.
Frequently asked questions about importing Excel into an R data frame
Does read_excel return an R data frame?
read_excel() returns a tibble. A tibble inherits from data.frame and works with most functions that accept an R data frame. Use as.data.frame() when a plain base R data frame is required.
How do I import an XLSX file into RStudio?
Install and load readxl, then run read_excel("path/to/file.xlsx"). RStudio is the development environment; the actual import is performed by R and the readxl package.
How do I import a specific Excel sheet into R?
Use the sheet argument with either a worksheet name or number, such as read_excel("report.xlsx", sheet = "Sales") or read_excel("report.xlsx", sheet = 2).
Can R import both XLS and XLSX files?
Yes. read_excel() supports both formats and detects the workbook type from the file extension. The package also provides read_xls() and read_xlsx() for format-specific imports.
How do I preserve Excel ID values with leading zeros?
Import the ID column as text by setting its corresponding col_types value to "text". Importing it as numeric would remove leading zeros.
Editorial QA checklist for the R Excel data-frame import
- Verify that the example workbook path uses valid forward slashes or escaped backslashes.
- Confirm that each named worksheet exists in the workbook used to test the code.
- Check that the imported row count and column count match the source Excel range.
- Confirm that identifiers, dates, and numeric values are assigned suitable R column types.
- Test the examples with both an XLSX workbook and an older XLS workbook where both formats are discussed.
R Excel-to-data-frame import summary
Use readxl::read_excel() to import an XLS or XLSX worksheet into R. The result is a tibble that can usually be analyzed directly. Use as.data.frame() or data.frame() when a base R data frame is specifically needed. Arguments such as sheet, range, skip, n_max, col_names, col_types, and na provide control over which Excel data is imported and how it is represented in R.
TutorialKart.com