[R] displaying computation outputs inside "for" loops

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Mon Aug 9 11:26:08 CEST 2004

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.


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/

----- 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 it
is so
> simple. There must be something I am missing in the way they are
> 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
> 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
> 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
> 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
> 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
> 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
> ______________________________________________
> 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!

More information about the R-help mailing list