R Read all files and statistics under the path

Source: Internet
Author: User


# # # # #合并表 #######
RM (List=ls ())
Library (READR)
Library (rpivottable)
Library (Lubridate)
Library (READXL)
Library (DPLYR)
Library (Tidyr)


#---------------------Merge Indexes---------------------




a1= list.files ("E:/input") #list. Files command to enter all file names under the input folder a


SETWD ("e:/input/")


b1= list.files (a1[1],pattern = "index.")
N=length (A1)



For (i in 2:n) {
m1= list.files (a1[i],pattern = "index.")

#m1 <-as.character (M1)
#m11 <-grep ("^ cable", m1,value=t)
B1<-rbind (B1,M1)
}
SETWD ("e:/")
dir = Paste ("./input/", A1, "/", b1,sep= "") #用paste命令构建路径变量dir
n = Length (dir) #读取dir长度, which is the number of files in the folder
Merge.data1 = Read_excel (path= dir[1],col_names = T) #读入第一个文件内容 (you can not read one first, but for the sake of simplicity, omitting the time to define Data.frame, I choose to read into a file first.)
Merge.data<-merge.data1%>%select (Cust_num,cust_id,ac_num,cardhold, commissioned date, COLL, collection records, reminders style, external visit, external photos, customer signed documents)


For (i in 2:n) {

New.data= read_excel (path= dir[i],col_names = T)
New.data1<-new.data%>%select (Cust_num,cust_id,ac_num,cardhold, commissioned date, COLL, collection records, reminders style, external visit, external photos, customer signed documents)
Merge.data = Rbind (merge.data,new.data1)
}




#循环从第二个文件开始读入所有文件, and combine into the merge.data variable
#write. xlsx (merge.data, File = "E:\\output\\suoyin.xlsx", SheetName = "Testsheet", Row.names = FALSE)
Write.csv (Merge.data,file = "./output/suoyin.csv", row.names=f) #输出组合后的文件merge. csv to input file
#---------------------Count the number of indexes-----------------


Count.data1 = Read_excel (path= dir[1],col_names = T)
Count<-nrow (COUNT.DATA1)




For (i in 2:n) {
New.data= read_excel (path= dir[i],col_names = T)
Count1<-nrow (New.data)
Count<-rbind (COUNT,COUNT1)
}


Cc<-paste ("E:/input", a1[1], "******", Sep = "/")
c1= list.files (CC)
Lc1=length (C1)
For (i in 2:n) {
Cc1<-paste ("E:/input", a1[i], "******", Sep = "/")
cm= List.files (CC1)
Lcm<-length (CM)
Lc1<-rbind (LC1,LCM)
}


Counttest<-data.frame (A1,COUNT,LC1)
Names (counttest) <-c ("branch", "Index Amount", "Reminder Amount")


Write.csv (Counttest,file = "./output/count.csv", row.names=f)


#-----------------------List of outbound visitors------------------------


SETWD ("e:/input/")
wb1= list.files (a1[1],pattern = "Outbound visit list.")


For (i in 2:n) {
wm1= list.files (a1[i],pattern = "Outbound visit list.")

#m1 <-as.character (M1)
#m11 <-grep ("^ cable", m1,value=t)
Wb1<-rbind (WB1,WM1)
}
SETWD ("e:/")
Wdir = Paste ("./input/", A1, "/", wb1,sep= "") #用paste命令构建路径变量dir
WN = Length (Wdir) #读取dir长度, which is the number of files in the folder
Wmerge.data1 = Read_excel (path= wdir[1],col_names = T) #读入第一个文件内容 (you can not read one first, but for the sake of simplicity, omitting the time to define Data.frame, I choose to read into a file first.)
Wmerge.data<-wmerge.data1%>%select (serial number, customer ID, customer name, customer address, customer status statement, customer's results, collector, date)


For (i in 2:n) {

Wnew.data= read_excel (path= wdir[i],col_names = T)
Wnew.data1<-wnew.data%>%select (serial number, customer ID, customer name, customer address, customer status statement, customer's results, collector, date)
Wmerge.data = Rbind (wmerge.data,wnew.data1)
}




#循环从第二个文件开始读入所有文件, and combine into the merge.data variable
#write. xlsx (merge.data, File = "E:\\output\\suoyin.xlsx", SheetName = "Testsheet", Row.names = FALSE)
Write.csv (Wmerge.data,file = "./output/wf.csv", row.names=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.