Dear R-users, I am puzzled by for loops and am kind of ashamed to ask because it is so simple. There must be something I am missing in the way they are executed. Basically, I would like to iterate a given number of time and generate a bunch of stats (that's what loops are designed for, right?). Before doing this I simply want to test simple procedure and see if they work (ie got the syntax right - my main problem as I am new to R - and produce expected results). Even for something as basic as for (i in 1:3) {i} I get no screen output. Shouldn't R systematically display i for every loop just like I am requesting with invoking "i"? When checking at the end of the looping, i is indeed assigned to 5 but I cannot get intermediate values. Further testing shows that i takes all the values in turn.> for (i in 1:3) {str(i)}int 1 int 2 int 3 but summary(i) doesn't display anything. Isn't there something weird with this? Am I expecting something wrong and for loops just don't work that way, unless using str() command? I tried print() and other descriptive commands but to no avail. A quick explanation would be grately appreciated Thomas *** Le contenu de cet e-mail et de ses pi??ces jointes est destin...{{dropped}}
Dimitris Rizopoulos
2004-Aug-09 09:26 UTC
[R] displaying computation outputs inside "for" loops
Hi Thomas, is this what you would like to get, for(i in 1:3){ x <- rnorm(5) cat("the values of `x' are:", format(x), "\n") cat("computation", i, "finished\n\n") } for(i in 1:3) print(i) I hope this helps. Best, Dimitris ---- Dimitris Rizopoulos Doctoral Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Dewez Thomas" <t.dewez at brgm.fr> To: "'R mailing list'" <r-help at stat.math.ethz.ch> Sent: Monday, August 09, 2004 11:15 AM Subject: [R] displaying computation outputs inside "for" loops> Dear R-users, > > I am puzzled by for loops and am kind of ashamed to ask because itis so> simple. There must be something I am missing in the way they areexecuted.> > Basically, I would like to iterate a given number of time andgenerate a> bunch of stats (that's what loops are designed for, right?). Beforedoing> this I simply want to test simple procedure and see if they work (iegot the> syntax right - my main problem as I am new to R - and produceexpected> results). > > Even for something as basic as > for (i in 1:3) {i} > > I get no screen output. Shouldn't R systematically display i forevery loop> just like I am requesting with invoking "i"? When checking at theend of the> looping, i is indeed assigned to 5 but I cannot get intermediatevalues.> > Further testing shows that i takes all the values in turn. > > for (i in 1:3) {str(i)} > int 1 > int 2 > int 3 > > but summary(i) doesn't display anything. Isn't there something weirdwith> this? Am I expecting something wrong and for loops just don't workthat way,> unless using str() command? I tried print() and other descriptivecommands> but to no avail. > > A quick explanation would be grately appreciated > > Thomas > *** > Le contenu de cet e-mail et de ses pi??ces jointes estdestin...{{dropped}}> > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html
On Mon, 9 Aug 2004, Dewez Thomas wrote:> Dear R-users, > > I am puzzled by for loops and am kind of ashamed to ask because it is so > simple. There must be something I am missing in the way they are executed. >If you want an explanation, rather than just a way to get printing, look at FAQ 7.18. -thomas