I am trying to load binary files in the following fashion load("pred/Pred_pres_a_indpdt") load("pred/Pred_pres_b_indpdt") load("pred/Pred_pres_c_indpdt") load("pred/Pred_pres_d_indpdt") load("pred/Pred_pres_e_indpdt") load("pred/Pred_pres_f_indpdt") but I would like to set up a for loop to replace the letters a:f Here is what I have so far: letter=c("a","b","c","d","e","f") for(l in letter){ cat("load('pred/Pred_pres_",l,"_indpdt')",sep="",collapse="","\n") } That will print the above commands to the console but it will not actually enter the commands. Any ideas? Thanks Chris -- View this message in context: http://www.nabble.com/for-loop-for-file-names-tp24746012p24746012.html Sent from the R help mailing list archive at Nabble.com.
Dear Chris, Try this: x <- c("a","b","c","d","e","f") sapply(x, function(i){ i <- paste("pred/Pred_pres_",i,"_indpdt", sep ="") load(i) } ) HTH, Jorge On Thu, Jul 30, 2009 at 4:06 PM, waltzmiester <cwalte03@shepherd.edu> wrote:> > I am trying to load binary files in the following fashion > > load("pred/Pred_pres_a_indpdt") > load("pred/Pred_pres_b_indpdt") > load("pred/Pred_pres_c_indpdt") > load("pred/Pred_pres_d_indpdt") > load("pred/Pred_pres_e_indpdt") > load("pred/Pred_pres_f_indpdt") > > but I would like to set up a for loop to replace the letters a:f > > Here is what I have so far: > > letter=c("a","b","c","d","e","f") > > for(l in letter){ > > cat("load('pred/Pred_pres_",l,"_indpdt')",sep="",collapse="","\n") > > } > > That will print the above commands to the console but it will not actually > enter the commands. > > Any ideas? > > Thanks > > Chris > -- > View this message in context: > http://www.nabble.com/for-loop-for-file-names-tp24746012p24746012.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]
Try this, files = paste('pred/Pred_pres_', letters[1:6], '_indpdt',sep="") lapply(files, load) HTH, baptiste 2009/7/30 waltzmiester <cwalte03 at shepherd.edu>:> > I am trying to load binary files in the following fashion > > load("pred/Pred_pres_a_indpdt") > load("pred/Pred_pres_b_indpdt") > load("pred/Pred_pres_c_indpdt") > load("pred/Pred_pres_d_indpdt") > load("pred/Pred_pres_e_indpdt") > load("pred/Pred_pres_f_indpdt") > > but I would like to set up a for loop to replace the letters a:f > > Here is what I have so far: > > letter=c("a","b","c","d","e","f") > > for(l in letter){ > > ? ? ? ?cat("load('pred/Pred_pres_",l,"_indpdt')",sep="",collapse="","\n") > > } > > That will print the above commands to the console but it will not actually > enter the commands. > > Any ideas? > > Thanks > > Chris > -- > View this message in context: http://www.nabble.com/for-loop-for-file-names-tp24746012p24746012.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. >-- _____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK http://newton.ex.ac.uk/research/emag
Try this: sapply(sprintf("pred/Pred_pres_%s_indpt", x), load, envir = .GlobalEnv) You need set the envir argument to global environment inside the sapply. On Thu, Jul 30, 2009 at 5:06 PM, waltzmiester <cwalte03@shepherd.edu> wrote:> > I am trying to load binary files in the following fashion > > load("pred/Pred_pres_a_indpdt") > load("pred/Pred_pres_b_indpdt") > load("pred/Pred_pres_c_indpdt") > load("pred/Pred_pres_d_indpdt") > load("pred/Pred_pres_e_indpdt") > load("pred/Pred_pres_f_indpdt") > > but I would like to set up a for loop to replace the letters a:f > > Here is what I have so far: > > letter=c("a","b","c","d","e","f") > > for(l in letter){ > > cat("load('pred/Pred_pres_",l,"_indpdt')",sep="",collapse="","\n") > > } > > That will print the above commands to the console but it will not actually > enter the commands. > > Any ideas? > > Thanks > > Chris > -- > View this message in context: > http://www.nabble.com/for-loop-for-file-names-tp24746012p24746012.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]