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

What are the practical skills of importing and exporting Excel files in batches with openxlsx packages

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This issue of the content of the editor will bring you about the openxlsx package batch import and export Excel file what is the practical skills, the article is rich in content and professional analysis and description for you, after reading this article, I hope you can get something.

R is a free, free, open source software, mainly used for statistical analysis, calculation and statistical mapping, its diversified functions mainly rely on R package, this column will introduce practical R package tools for everyone to explain the operation of the package in detail, to facilitate everyone to learn and practice the R language. The following mainly introduces the tools for importing and exporting excel files: openxlsx package, as well as some tips for bulk import and export files. In the previous push, we introduced the import and export of Excel files [R language Learning 6] R data import and export methods. Today, we mainly introduce the specific use of openxlsx package. The first is the installation and loading of the package, which is currently included on the CRAN platform, so you can refer to the method we shared earlier [R language Learning 3] R language package source and usage introduction to install install.packages ("openxlsx") library ("openxlsx") > >

Export excel fil

Export excel file using the write.xlsx function, through the introduction we can see that this function is mainly to export the data box to xlsx file, and because excel can have multiple sheet, this function also supports the export of a list of data.frames.

So the command is as follows:

L >

Import excel Fil

The file is imported using the read.xlsx function, and the method is also very simple, mainly setting the file location of excel and the sheet name or order (if there are multiple sheet)

For example, to import the first sheet in the excel file we just created, the command is

Iris > >

Batch import of multiple sheet in excel files

The above command can only import one sheet of excel. In the actual process, you may need to import more than one sheet at a time, then you can use a loop function:

Data.list

Import multiple excel files in bulk

Sometimes you need to import multiple files in the directory into R at once, you can use the following command, but it should be noted that this command only supports one sheet per excel.

Myexcelfile = list.files (pattern= "* .xlsx") # list.files function list2env (lapply (setNames (myexcelfile, make.names (gsub ("* .xlsx $", "", myexcelfile)), read.xlsx), envir = .GlobalEnv) # matches the file name ending in .xlsx, replacing it with empty, that is, removing .xlsx, leaving only the file name as the variable name > >

Import multiple txt or excel files in bulk

Similarly, we can use this command to import csv or txt documents in batches.

Mytxtfile = list.files (pattern= "* .txt") # generate a list of .txt files list2env (lapply (setNames (mytxtfile, make.names (gsub ("* .txt $", ", mytxtfile)), read.table,header=TRUE,sep="\ t "), envir = .GlobalEnv) mycsvfile = list.files (pattern=" * .csv ") # generate a list of .csv files list2env (setNames (mycsvfile, make.names (gsub (" * .csv $"," (mycsvfile)), read.csv,header=TRUE,check.names=FALSE), envir = .GlobalEnv) these are the practical skills of importing and exporting Excel files in batches of openxlsx packages shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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: 290

*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

Internet Technology

Wechat

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

12
Report