Hello R people! How can one use a for loop (or something similar) in R? As I type in each line, I get syntax errors... I'm just confused how much to type in at each ">" prompt. Thanks for your help, cathy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Catherine M. Stein Research Assistant, Tuberculosis Research Unit Doctoral Candidate in Genetic Epidemiology Department of Epidemiology and Biostatistics Case Western Reserve University office: (216)368-0875 or (216)778-1378 e-mail: kasia at darwin.cwru.edu, or cmstein at cwru.edu http://darwin.cwru.edu/~kasia EPBI Student Resources Page: http://hal.epbi.cwru.edu/stures/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Mon, 2004-02-02 at 13:42, Catherine Stein wrote:> Hello R people! > > How can one use a for loop (or something similar) in R? As I type in each > line, I get syntax errors... I'm just confused how much to type in at each > ">" prompt. > > Thanks for your help, > cathyHello. I believe you want something like: for(i in 1:n){ ...some lines here... } If you have only one line within the for loop you can use it without the brackets. If the environment is still open (as within the for loop) you will not get a new ">" prompt but a "+" prompt to continue as if you were writing on the same line. Only when you close the brackets the prompt will return to ">" after executing the for loop. Hope this will help. Pedro -- ----------------------------------------------------------- Pedro Pereira Rodrigues http://www.dcc.fc.up.pt/~prodrigues/ Phone: +351 226078830 - Ext: 121 Snail: Department of Computer Science Faculty of Sciences - University of Porto Rua do Campo Alegre, 823 4150-180 Porto - Portugal
Catherine Stein wrote:> How can one use a for loop (or something similar) in R? As I type in each > line, I get syntax errors... I'm just confused how much to type in at each > ">" prompt. >Have you read help("for") (you need to quote 'for' here to avoid a syntax error!)? If you'd shown us exactly what you'd typed we could probably help better. Suppose you want to loop from 1 to 10 and print it. You can do the following, where '>' is the R prompt (dont type it): > for(i in 1:10)print(i) - ie all on one line > for(i in 1:10){print(i)} - with curly brackets > for(i in 1:10) + print(i) - where '+' is the continuation prompt (dont type it) - R gives you this when it realises you havent written a complete expression yet. > for(i in 1:10) { + print(i) + } - curly brackets enclose as many expressions as you like inside the loop. > for(i in 1:10) + { print(i) + } - curly brackets anywhere. R works it out. You can even do, and I didn't think this would work... > for(i in + 1:10) + {print(i)} So in short, I cant get it to give a syntax error :) What are you doing? What version of R, and what platform? Baz
Hi On 2 Feb 2004 at 8:42, Catherine Stein wrote:> > Hello R people! > > How can one use a for loop (or something similar) in R? As I type in > each line, I get syntax errors... I'm just confused how much to typeUse curly braces for (i in ...) { your long commands in several lines } "An Introduction to R" in doc directory is your best friend. Cheers Petr> in at each ">" prompt. > > Thanks for your help, > cathy > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > ~~~~~~~~~ Catherine M. Stein Research Assistant, Tuberculosis Research > Unit Doctoral Candidate in Genetic Epidemiology Department of > Epidemiology and Biostatistics Case Western Reserve University office: > (216)368-0875 or (216)778-1378 e-mail: kasia at darwin.cwru.edu, or > cmstein at cwru.edu http://darwin.cwru.edu/~kasia > > EPBI Student Resources Page: > http://hal.epbi.cwru.edu/stures/ > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > ~~~~~~ > > ______________________________________________ > 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.htmlPetr Pikal petr.pikal at precheza.cz
Hello Cathy you should open some curly brackets after the "for" statement e.g. for(i in 1:1000){ print("Hello!") print(sum(15+i*3)) } or simlarly. But please keep in mind that R is rather slow with loops. So have a look at apply & friends and check if you can rephrase your problem to use them. cheers, Winni On Mon, 2004-02-02 at 14:42, Catherine Stein wrote:> > Hello R people! > > How can one use a for loop (or something similar) in R? As I type in each > line, I get syntax errors... I'm just confused how much to type in at each > ">" prompt. > > Thanks for your help, > cathy > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > Catherine M. Stein > Research Assistant, Tuberculosis Research Unit > Doctoral Candidate in Genetic Epidemiology > Department of Epidemiology and Biostatistics > Case Western Reserve University > office: (216)368-0875 or (216)778-1378 > e-mail: kasia at darwin.cwru.edu, or cmstein at cwru.edu > http://darwin.cwru.edu/~kasia > > EPBI Student Resources Page: > http://hal.epbi.cwru.edu/stures/ > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > ______________________________________________ > 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-- --------------------------------------------------------------------- Dipl.-Math. Winfried Theis SFB 475, Fachbereich Statistik, Universit"at Dortmund, 44221 Dortmund Tel.: +49-231-755-5903 FAX: +49-231-755-4387