[R] Automation: Batch mode or Loop?

jim holtman jholtman at gmail.com
Sat Mar 29 02:05:15 CET 2008

You can do it interactively with a script.  If you know how large your
output will be, you can preallocate an object and then just store the
results in it.  The loop would look something like this:

files <- Sys.glob("*.csv")  # get names of files to process
result <- numeric(length(files))  # preallocate assuming single value
from each file
for (i in seq_along(files)){
    input <- read.csv(files[i])  # or however you want to read it
    ... computations  ...
    result[i] <- answer

On Fri, Mar 28, 2008 at 7:42 PM, Stropharia
<stevenworthington at hotmail.com> wrote:
> R users,
> I've been searching around for a solution to this for a few days, any help
> would be appreciated.
> I have working R code (a custom function) that transforms 3D coordinate data
> into angles (between any three sets of coordinates). The code outputs a
> single scalar (an angle). For my research I will have several hundred .csv
> files containing coordinate data - each file is a set of coordinates on an
> organism. I would like to run the code on each of these files in turn and
> then output the resulting angles to a new .csv file, appending that file
> with a new scalar each time the code completes it's operation. Ideally, at
> the end I would have a new .csv file with a single column with several
> hundred scalars - which are the results of the analysis on each individual
> coordinate data file.
> I am using R on OS X (Leopard), ver. 2.6.2. I generally use the console.
> Is it possible to automate this series of operations, so that several
> hundred .csv coordinate data files placed in one folder could be
> sequentially analyzed? If so, would it be best to do this in "Batch Mode" or
> interactively using a loop? I don't understand how to run batch mode from
> the terminal on OS X (online instructions seem to be for linux or windows).
> I've read about using the Source and Sink commands, but this seems only for
> interactive use, and sink seems to output only to .txt files. I have tried
> output as write.csv and this works for a single file, but I don't understand
> how to automate it. Thanks.
> Steve
> --
> View this message in context: http://www.nabble.com/Automation%3A-Batch-mode-or-Loop--tp16365096p16365096.html
> Sent from the R help mailing list archive at Nabble.com.
> ______________________________________________
> 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.

Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

More information about the R-help mailing list