[R] Assignments in loops

Andy Bunn abunn at montana.edu
Tue Dec 30 02:33:59 CET 2003


Try assign - a la the subject heading.

I think this should work.

carmakes <- c('BMW','Chrysler','Citroen','Fiat','Ford','Holden','Honda',
 
'Mercedes','MG','Mitsubishi','Nissan','Peugeot','Renault','Subaru','Toyo
ta','VW')
for (i in 1:length(carmakes)) {
   # Create your path and file name
   fyle <- paste("c:/data/cars03/",carmakes[i],".txt",sep="")
   # Read fyle into a temp variable  - replace everyhting after
   # <- with your read.table command.
   temp <- data.frame(x = runif(10), y = runif(10), z = runif(10))
   # Use assign to create the object
   assign(carmakes[i], temp)
}

HTH, 
Andy




More information about the R-help mailing list