Suphajak Ngamlak
2010-Sep-15 08:55 UTC
[R] Get File Names in Folder, Read Files, Update, and Write
Dear All, Could you please recommend how I can do this? I have several text files in one folder. Let's name them A0801.RSK, A0802.RSK, .... I would like R to 1) Know all file names in this folder 2) Update value in one column of these files 3) Write results in another text file with _xval in the file names Below is R code for read, update, and write one file Import<-"C:/A0810.RSK" Table<-read.table(file= Import, sep = ",", head=TRUE, na.strings = "NA") Table$VALUE <-0 Export<-"C:/A_XVal0810.RSK" write.table(Table, file= Export, sep = ",", col.names = TRUE) Thank you Best Regards, Suphajak Ngamlak Equity and Derivatives Trading Phatra Securities Public Company Limited Tel: +662-305-9179 Email: suphajak@phatrasecurities.com [[alternative HTML version deleted]]
Uwe Ligges
2010-Sep-15 09:00 UTC
[R] Get File Names in Folder, Read Files, Update, and Write
On 15.09.2010 10:55, Suphajak Ngamlak wrote:> Dear All, > > > > Could you please recommend how I can do this? > > > > I have several text files in one folder. Let's name them A0801.RSK, > A0802.RSK, .... > > I would like R to > > 1) Know all file names in this folder?list.files> 2) Update value in one column of these files > 3) Write results in another text file with _xval in the file names?paste Uwe Ligges> > > Below is R code for read, update, and write one file > > > > Import<-"C:/A0810.RSK" > > Table<-read.table(file= Import, sep = ",", head=TRUE, na.strings = "NA") > > Table$VALUE<-0 > > Export<-"C:/A_XVal0810.RSK" > > write.table(Table, file= Export, sep = ",", col.names = TRUE) > > > > Thank you > > Best Regards, > > Suphajak Ngamlak > Equity and Derivatives Trading > Phatra Securities Public Company Limited > Tel: +662-305-9179 > Email: suphajak at phatrasecurities.com > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
steven mosher
2010-Sep-16 05:28 UTC
[R] Get File Names in Folder, Read Files, Update, and Write
you are welcomed. Steve On Wed, Sep 15, 2010 at 6:11 PM, Suphajak Ngamlak < Suphajak@phatrasecurities.com> wrote:> Thank you so much. It works well > > > > Best Regards, > > Suphajak Ngamlak > Equity and Derivatives Trading > Phatra Securities Public Company Limited > Tel: +662-305-9179 > Email: suphajak@phatrasecurities.com > > *From:* steven mosher [mailto:moshersteven@gmail.com] > *Sent:* Thursday, September 16, 2010 2:29 AM > *To:* Suphajak Ngamlak > *Subject:* Re: [R] Get File Names in Folder, Read Files, Update, and Write > > > > > Import<-"C:/A0810.RSK" > > Table<-read.table(file= Import, sep = ",", head=TRUE, na.strings = "NA") > > Table$VALUE <-0 > > Export<-"C:/A_XVal0810.RSK" > > write.table(Table, file= Export, sep = ",", col.names = TRUE) > > > As uwe, suggests list.files() or you can use dir() > > A robust way to do this would use R.utils package ( see Cran) > but this is only necessary if you want to move the folder and still > have the code work. it makes the code work regardless of your working > directory > > assuming you know the name of the folder and its unique. > > insertString <- "_XVal" > targetFolder <- "yourfoldername" > > > > folderPath <- getAbsolutePath(targetFolder) #see R.utils > outputFolder <- folderpath # you could create a different output folder > name > > # only grab the .RSK files using a regular expression > > fullFilenames <- list.files(path=folderPath, > full.names=TRUE,pattern="(.RSK)") > > # get only the file names for modification > filenames <- list.files(path=folderPath, > full.names=FALSE,pattern="(.RSK)") > > for(filenumber in 1:length(fullFilenames)) { > Table<-read.table(file= fullFilenames[filenumber], sep = ",", > head=TRUE, na.strings = "NA") > Table$VALUE <-0 > outfileName <- paste(substr(filenames[filenumber]1,1), > insertString, > > substr(filenames[filenumber],2,nchar(filenames[filenumber]), > sep="") > outFilePath > <-file.path(outputFolder,outfileName,fsep=.Platform$file.sep) > write.table(Table, file= outFilePath, sep = ",", col.names = TRUE) > > } > > > > On Wed, Sep 15, 2010 at 1:55 AM, Suphajak Ngamlak < > Suphajak@phatrasecurities.com> wrote: > > Dear All, > > > > Could you please recommend how I can do this? > > > > I have several text files in one folder. Let's name them A0801.RSK, > A0802.RSK, .... > > I would like R to > > 1) Know all file names in this folder > > 2) Update value in one column of these files > > 3) Write results in another text file with _xval in the file names > > > > Below is R code for read, update, and write one file > > > > Import<-"C:/A0810.RSK" > > Table<-read.table(file= Import, sep = ",", head=TRUE, na.strings = "NA") > > Table$VALUE <-0 > > Export<-"C:/A_XVal0810.RSK" > > write.table(Table, file= Export, sep = ",", col.names = TRUE) > > > > Thank you > > Best Regards, > > Suphajak Ngamlak > Equity and Derivatives Trading > Phatra Securities Public Company Limited > Tel: +662-305-9179 > Email: suphajak@phatrasecurities.com > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > >[[alternative HTML version deleted]]