R Language-Basic operations (bulk data read and output)

Source: Internet
Author: User

One common problem with the R language is the bulk reading of files and the batch output of the results. Bulk Read

Bulk read data, there are two forms, read all the files in a directory, read multiple tables from the database.

# #读取同一目录下的所有文件
path <-"F:/rfile/od-b/data" # #文件目录
fileNames <-dir (path)  # #获取该路径下的文件名
FilePath <-sapply (fileNames, function (x) { 
                 paste (path,x,sep= '/')})   # #生成读取文件路径
data <-lapply ( FilePath, function (x) {
             read.csv (x, Header=t)})  # #读取数据, the result is a list

# #从数据库中读取数据类似上面, gets the file name in the database, Write a regular filter file name after the for loop read.
Batch Output

Batch output CSV file for results, where data is list format

Outpath <-"F:/rfile/od-b/consequence" # #输出路径
out_filename <-sapply (names (data), function (x) {
                    paste (x, ". csv", sep= ')}) # #csv格式
out_filepath  <-sapply (out_filename, function (x) {
                     paste (Outpath, x,sep= '/')}) # #输出路径名
# #输出文件 for
(i in 1:length (data)) {
  write.csv (Data[[i]], file=out_filepath[i], row.name=f) 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.