需要在文件夹中的所有文本文件上运行R代码

时间:2016-06-06 07:45:28

标签: r file

我有一个文本文件。我为它做了一个R代码,从中提取出一定数量的信息。

###Read file and format
txt_files <- list.files(pattern = '*.txt')
text <- lapply(txt_files, readLines)   
text <- sapply(text, function(x) iconv(x, "latin1", "ASCII", sub=""))

###Search and store grep
l =grep("words" ,text)
(k<- length(l))

###Matrix to store data created
mat <- matrix(data = NA, nrow = k, ncol = 2)
nrow(mat)

###Main
for(i in 1:k){                                          
u= 1
while(text[(l[i])-u]!=""){
line.num=u;
u=u+1
}
mat[i,2]<-text[(l[i])-u-1]
mat[i,1]<- i
}   
###Write the output file
write.csv(mat, file = "Evalutaion.csv")

一次只能在一个文件上运行。我需要在许多文件上运行它,并将所有结果附加到一个文件中,并附加一列,告诉我每个结果所来自的文件名。我无法想出一些解决方案。我做了什么改变?

1 个答案:

答案 0 :(得分:0)

将您的操作应用于文件夹中的所有文件:

   txt_files <- list.files(pattern = '*.txt')

   # Applying all your functions on all txt_files using for loop, you need to use indexes inside where ever you are using txt_files
   for (i in 1:length(txt_files)) {
   # Operation 1
   # Operation 2
   # Operation 3

   write.table(mat,file=paste0("./",sub(".txt","",FILES[i]),".csv"),row.names=F,quote=F,sep=",")
   }

将文件与相同的标头合并,我有两个csv文件,其中包含相同标题DataValue,文件名为File1.csvFile2.csv位于Header内文件夹,我合并在一起得到一个标题和所有行和列。确保两个文件具有相同数量的列和相同的标题。

## Read into a list of files, an Example below
 library(plyr)
 library(gdata)
 setwd("./Header")   # CSV Files to be merged are in this direcory
 ## Read into a list of files:
 filenames <- list.files(path="./",pattern="*.csv")
 fullpath=file.path("./",filenames)
 print (filenames)
 print (fullpath)
 dataset <- do.call("rbind",lapply(filenames,FUN=function(files){read.table(files,sep=",",header=T)}))
 dataset
#   Data Value
# 1  ABC    23
# 2  PQR    33
# 3  MNP    43       # Till here was File.csv
# 4   AC    24
# 5   PQ    34
# 6   MN    44       # Till here was File2.csv

write.table(dataset,file="dataset.csv",sep=",",quote=F,row.names=F,col.names=T)