Easy Tutorial
❮ R Vector R String ❯

R Excel File

Excel files are primarily in xls or xlsx format, which can be directly read in R using the xlsx library.

To read and write Excel files in R, you need to install an extension package. You can install it by entering the following command in the R console:

install.packages("xlsx", repos = "https://mirrors.ustc.edu.cn/CRAN/")

In fact, almost all Excel software and most spreadsheet software support CSV format data, so it is entirely possible to interact with R using CSV, eliminating the need to use Excel.

To check if xlsx is installed successfully:

Example

# Verify if the package is installed
any(grepl("xlsx", installed.packages()))
# Load the package
library("xlsx")
library("xlsx")

Executing the above code outputs the result:

[1] TRUE
Loading required package: rJava
Loading required package: methods
Loading required package: xlsxjars

Excel file data:

Click the link to download the Excel test data: https://static.tutorialpro.org/download/sites.xlsx

Next, we can use the read.xlsx() function to read the Excel data:

Example

# Read data from the first worksheet of sites.xlsx
data <- read.xlsx("sites.xlsx", sheetIndex = 1)
print(data)
❮ R Vector R String ❯