Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R Excel Files

R Excel Files

Excel files are mainly in xls or xlsx format; both can be read directly in R by importing the xlsx library.

Reading and writing Excel files in R requires installing an extension package; we can enter the following command in the R console to install it:



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

The installation process is as follows:

In fact, almost all Excel software, like most spreadsheet software, supports CSV format data, so you can interact with R entirely through CSV; there is no need to use Excel.

Check whether xlsx was installed successfully:

Example

Verify whether the package is installed

any

(

grepl

(

"xlsx"

,

installed.packages

(

)

)

)

Load the package

library

(

"xlsx"

)

library

(

"xlsx"

)

The output of the code above is:

[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.jyshare.com/download/sites.xlsx

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

Example

Read the data of the first worksheet in sites.xlsx

data

<-

read.

xlsx

(

"sites.xlsx"

, sheetIndex

=

1

)

print

(

data

)