-1

I have many .csv files in a folder. I want to get the binning result from each of the .csv file one by one automatically by R scripting from command line, and one by one write the result of all files into result.csv file. For example, I have file01.csv, file02.csv, file03.csv, file04.csv, file05.csv. I want that first R script will read / execute file01.csv and write the result into result.csv file, then read / execute file02.csv and write result into result.csv, again read / execute file03.csv and write result into result.csv, and so on. This is like a loop on all the files, and I want to execute the R script from the command line.

Here is my starting R script:

data <- read.table("file01.csv",sep=",",header = T)

df.train <- data.frame(data)

library(smbinning) # Install if necessary

<p>#Analysis by dwell:</p>

df.train_amp <-
rbind(df.train)

res.bin <- smbinning(df=df.train_amp, y="cvflg",x="dwell")

res.bin      #Result

<p># Analysis by pv</p>
df.train_amp <-
rbind(df.train)

res.bin <- smbinning(df=df.train_amp, y="cvflg",x="pv")

res.bin   #Result

Any suggestion and support would be appreciated highly.

Thank

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
Tofazzal
  • 71
  • 8

2 Answers2

1

Firstly you will want to read in the files from your directory. Place all of your source files in the same source directory. I am assuming here that your CSV files all have the same shape. Also, I am doing nothing about headers here.

directory <- "C://temp"  ## for example
filenames <- list.files(directory, pattern = "*.csv", full.names = TRUE)
# If you need full paths then change the above to 
# filenames <- list.files(directory, pattern = "*.csv", full.names = TRUE)

bigDF <- data.frame()
for (f in 1:length(filenames)){
    tmp <- read.csv(paste(filenames[f]), stringsAsFactors = FALSE)
    bigDF <- rbind(bigDF, tmp)
}

This will add the rows in tmp to bigDF for each read, and should result in final bigDF.

To write the df to a csv is trivial in R as well. Anything like

# Write to a file, suppress row names
write.csv(bigDF, "myData.csv", row.names=FALSE)

# Same, except that instead of "NA", output blank cells
write.csv(bigDF, "myData.csv", row.names=FALSE, na="")

# Use tabs, suppress row names and column names
write.table(bigDF, "myData.csv", sep="\t", row.names=FALSE, col.names=FALSE) 
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
  • Dear Shawn Mehan, Thank you very much. I thing my explanation was not enought, so I have revised the question. What I want is, for example, I have file01.csv, file02.csv, file03.csv, file04.csv, file05.csv. I want that first R script will read / execute file01.csv and write the result into result.csv file, then read / execute file02.csv and write result into result.csv, again read / execute file03.csv and write result into result.csv, and so on. This is like a loop on all the files, and I want to execute the R script from the command line. Could you please take a look. – Tofazzal Dec 01 '15 at 01:42
  • Yes, I want to do this from command line. Thank you for understanding me. – Tofazzal Dec 01 '15 at 01:44
  • Thanks, @RichardScriven. This will do that, save one need substitute `result.csv` for `bigDF`. You can write the script and run it from the command line, you can look at http://stackoverflow.com/questions/18306362/run-r-script-from-command-line. Just save the contents of this into a file and run the file through R. What am I missing? – Shawn Mehan Dec 01 '15 at 01:46
  • Thanks, I am going to execute the code according to your suggestion. Please keep in touch, I many need your support and suggestion again if I find any difficulties. Thank you. – Tofazzal Dec 01 '15 at 02:02
  • If it works, then you can come back and tell us by accepting the answer (the check next to the question). Good luck. – Shawn Mehan Dec 01 '15 at 02:16
  • Dear Shawn Mehan, I cannot mach / adjust my code with your samples. I have added my R script in the question. Could you please take a look on how i can adjust my R script with your sample code? Appreciate it highly. Thank you. – Tofazzal Dec 01 '15 at 02:39
  • What you have above is not directly related. You probably want to collapse all of your different CSV into a single CSV, then read that one, say `bigDF.csv` into the above file. Save the one I gave you as a different file and run that. You will need to edit things like the directory first, before you run. – Shawn Mehan Dec 01 '15 at 02:44
0

Finally I find the above problem can be solved as follows:

library(smbinning) #Install if necessary。

files <- list.files(pattern = ".csv") ## creates a vector with all files names in your folder

cutpoint <- rep(0,length(files))

for(i in 1:length(files)){

data <- read.csv(files[i],header=T)

df.train <- data.frame(data)

df.train_amp <- rbind(df.train,df.train,df.train,df.train,df.train,df.train,df.train,df.train) # Just to multiply the data

cutpoint[i] <- smbinning(df=df.train_amp, y="cvflg",x="dwell") # smbinning is calculated here

}

result <- cbind(files,cutpoint) # Produce details results

result <- cbind(files,bands) # Produce bands results

write.csv(result,"result_dwell.csv") # write result into csv file

Tofazzal
  • 71
  • 8