A safer way is: for (i in seq_along(VarNames)){ ....
What happens when VarNames is length zero: > x <- NULL > 1:length(x) [1] 1 0 > seq_along(x) integer(0) > On Sun, Oct 12, 2008 at 11:00 AM, Ted Harding <[EMAIL PROTECTED]> wrote: > Following up on Barry's suggestion led on to a more straightforward > approach: > > VarNames <- NULL > # VarNames <- c(VarNames, "Var1") > # VarNames <- c(VarNames, "Var2") > # VarNames <- c(VarNames, "Var3") > # [...] > for(i in (1:length(VarNames))){ > VarName <- VarNames[i] > [...] > > Then any one or some of those lines can be uncommented. > > With thanks for the suggestions. > Ted. > > On 12-Oct-08 11:32:31, Barry Rowlingson wrote: >> 2008/10/12 Ted Harding <[EMAIL PROTECTED]>: >>> Hi Folks, >>> I'm wondering if there's a secret trick to achieve the following. >>> >>> I have some big code for analysis related to a named variable, >>> which will be one of several in the columns of a dataframe, and >>> I would like to be able to choose between a run for just one of >>> these variables, or a run which loops over them all. >>> >>> So, for a single run, I could have the following kind of thing >>> in a file code.R to be sourced by source("code.R"): >>> >>> # VarName <- "Var1" >>> VarName <- "Var2" >>> # VarName <- "Var3" >>> # VarName <- "Var4" >>> >>> ### CUT OUT LOOP >>> # VarNames <- c("Var1","Var2","Var3","Var4") >>> # for( VarName in VarNames ) { >>> >>> << Lots of code related to analysis of variable VarName >> >>> >>> ### CUT OUT END OF LOOP >>> # } >>> >>> which would do the single case for Var2. A run for a different VarName >>> would be done by editing the first block to select the new VarName. >>> >>> Now, when I want to run the loop over all VarNames, I could of course >>> likewise edit the file so as to uncomment the code which follows the >>> "CUT OUT ..." lines. But I would prefer to avoid having to do the >>> latter, and am therefore wondering if there is some way to >>> conditionally >>> skip over those bits of code. >> >> What's the problem with doing a loop over a single VarName in >> VarNames? I'd put something like: >> >> VarNames = c( >> "Var1", >> "Var2", >> "Var3" >> ) >> >> at the start of my code for easy editing, and then if I only wanted >> to do it for one VarName you can just do: >> >> VarNames = c( >>#"Var1", >> "Var2" >>#"Var3" >> ) >> >> - it's just a shame that R doesn't like trailing commas in c() calls, >> which would make it even easier. >> >> Of course the real way to do it is to rewrite it as a function and do >> foo(c("Var1","Var2")) as desired.... >> >> Barry >> >> ______________________________________________ >> 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. > > -------------------------------------------------------------------- > E-Mail: (Ted Harding) <[EMAIL PROTECTED]> > Fax-to-email: +44 (0)870 094 0861 > Date: 12-Oct-08 Time: 16:00:14 > ------------------------------ XFMail ------------------------------ > > ______________________________________________ > 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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ 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.