Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read xlsx files in R language

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to read xlsx files in R language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

About R language to read Excel files, more troublesome, I have always opposed reading xlsx files directly, because crawler data, generally saved in the format of csv files, or directly saved to the database, no one will save to the Excel file inside. Both txt text files and csv comma separator files can be read with R's own function. Even for files that are generally not too large, we can open them and save them as csv files. Of course, Excel is not necessarily 1 million for more than 1 million pieces of data, which is related to the amount of data per line. I have never seen millions of Excel files. Xlsx table files are generally used to do business reports, there should be no millions of rows of data!

R language reads csv file

Nonsense, here or introduce how to read and write xlsx files, after all, many people have obsessive-compulsive disorder, must directly read xlsx files, especially those who learn mathematics, willpower is very strong!

R language needs a third-party package xlsx package to process xlsx files, and the xlsx package relies on the rJava package, because the big cow who wrote the xlsx package does not want to repeat the wheel and directly calls the functions in the rJava package, while the rJava package requires a java environment, so to sum up, you need to complete the following three steps:

Installing jdk provides a development and running environment for the java language. In fact, all you need is the running environment jre

Install the rJava package

Install xlsx package

Install jdk

1. Go to the java official website to download the jdk installation package, either www.java.com or www.oracle.com. The relationship between java and jdk can be understood as the relationship between C language and VC++. I will explain it this way. With the installation package, you can install it, according to the installation wizard.

2. Configure java environment variables

Why do you need to configure environment variables? you need to enable the system to run the R language to find the java environment when java is needed.

My jdk version is jdk1.8.0_144

For example, my jdk installation path is: d:\ Program Files\ Java\ jdk1.8.0_144\ bin

Right-click my computer = > Properties = > Advanced system Settings = = > Environment variables

Create a new system variable (user variable is also fine) JAVA_HOME=D:\ Program Files\ Java\ jdk1.8.0_144

Add% JAVA_HOME%\ bin;, at the beginning of the path variable to remember to write a semicolon to form a habit, so that the binary java.exe executable file under the bin directory will be known by the system, win+R key combination enter cmd, enter the command line, and then type java-version to display the jdk version, indicating that the java environment is configured successfully!

Install the rjava package

Install xlsx package

Conclusion: as long as the java environment is successfully configured, the installation of rJava and xlsx packages will certainly be successful. I have done it several times for different systems without any accident. It's that simple.

Once installed, we can happily manipulate the xlsx file.

Import package

This xlsxjars package will be downloaded and imported automatically with the xlsx package, so don't worry.

There is a xlsx file under my D disk root directory. Read it:

The first parameter represents the file name, and the second represents the SheetIndex to be read, because some xlsx tables may have multiple sheet tables, like this:

You need to determine which table to read, because the second parameter is to specify the parameters of the table, so you don't need to write like this:

> dat write.xlsx (dat,'test2.xlsx')

A test2.xlsx file is generated right under the workspace D:\\, and the content is the data content of dat.

Or open it and take a look:

About R language reading xlsx files, so much, I strongly do not approve of direct reading, obsessive-compulsive disorder, labor-consuming, time-consuming, memory resources!

Suggestion: read the xlsx file and do this:

1), xlsx file is saved as csv file

2), R can read the csv file, read.csv (path,header)

This is the end of "how to read xlsx files in R language". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report