Skip to content

Automatizing Simulation in R

3 messages · Jeff Harring, David Winsemius, Don MacQueen

#
I have a question regarding automatizing simulation runs in R...

I have 500 external data sets for a simulation study that I would like 
to bring into R to analyze. They have the names data1.dat, data2.dat, 
..., dataN.dat

Is there a way to automatize the bringing in of these data sets in R 
using the read.table function within a looping cycle?

For example...

for (j in 1:N){
data_"j" = read.table("data"j".dat, header=F)

executable code
.
.
.
}

bring in the next data set.

SAS uses a ampersand "&" to automatize the process.

Example code:

data _NULL_;
   set final;
       filename out "C:\data&k..dat";
       file out;
       put variables;
run;

I would welcome any insight into how to address this issue.

Thank you.
Jeff Harring
#
On Nov 12, 2009, at 8:42 AM, Jeff Harring wrote:

            
Perhaps something like this:

for (j in 1:N){
assign(paste("data", j, sep="_") ,  read.table( paste("data",j,".dat",  
sep=""), header=F) }


Or even better might be to assign to a list which would augmentalize  
your future automatizabilationabilty:

flist <- list()
for (j in 1:N){
flist[[j]] <- read.table( paste("data",j,".dat", sep=""), header=F) }

-- 
David
#
I often use the paste() and assign() approach that David suggested.
Another approach is to read into R a vector of file names and then 
loop over that vector.
Along the lines of:

infiles <- list.files(pattern='data*.dat')
dlist <- list()
length(dlist) <- length(infiles)
names(dist) <- infiles

for (inf in infiles) {
     tmpd <- read.table(inf,  *other args*)

    ## if you need to keep copies within R of all 500 sets of data
     dlist[[inf]] <- tmpd

     ## data analasyis using tmpd or dlist[[inf]]

}

-Don
At 10:22 AM -0500 11/12/09, David Winsemius wrote: