[R] For Loop

J C Nash pro|jcn@@h @end|ng |rom gm@||@com
Mon Sep 24 18:36:07 CEST 2018


One issue I haven't seen mentioned (and apologize if I've missed it) is that
of making programs readable for long-term use. In the histoRicalg project to
try to document and test some of the codes from long ago that are the underpinnings
of some important R computations, things like the negative index approach require
what we might term "local knowledge" i.e., to R. In such cases the old fashioned
for loop is easier for humans to understand.

The i:j form is a bit easier.

Compromise is to comment, and if your code is EVER to be used later, especially
by non-R users, it is a good idea to do so.

i.e.,

 c1[-1] # does loop from 2 to end of vector

John Nash

histoRicalg links, to which all welcome:
https://gitlab.com/nashjc/histoRicalg
https://gitlab.com/nashjc/histoRicalg/wikis/home
https://lists.r-consortium.org/g/rconsortium-project-histoRicalg

On 2018-09-24 12:13 PM, MacQueen, Don via R-help wrote:
> In my opinion this is a pretty reasonable question for someone new to R.
> 
> Yes, it can be written without a for loop, and it would be better. Rich Heiberger gave a good solution early on, but I'd like to add an outline of the reasoning that leads to the solution.
> 
> You are taking the log of a ratio, and in the ratio, the numerator uses elements 2 through len, and the denominator uses elements 1 through (len-1). So, just write it that way:
> 
>    c1[2:len]/c1[1:(len-1)]
> 
> or, taking advantage of using negative numbers when indexing vectors,
> 
>    c1[-1]/c1[-len]
> 
> then take the log
> 
>    s <- log( c1[-1]/c1[-len] )
> 
> Comparing this with the loop version makes an example of why people say the R language is vectorized.
> 
> Do good R programmers make very little use of the for statement? Since R is vectorized, the for statement is necessary less often than in  non-vectorized languages. But "very little use" would be too broad a generalization. It will depend on what problems are being solved.
> 
> Finally, if using the loop in this case, it's true that s must exist before the statement is run. But that's not much of a problem. Just put
>   s <- numeric( len-1)
> before the loop.
> 
> --
> Don MacQueen
> Lawrence Livermore National Laboratory
> 7000 East Ave., L-627
> Livermore, CA 94550
> 925-423-1062
> Lab cell 925-724-7509
>  
>  
> 
> On 9/22/18, 2:16 PM, "R-help on behalf of rsherry8" <r-help-bounces using r-project.org on behalf of rsherry8 using comcast.net> wrote:
> 
>     
>     It is my impression that good R programmers make very little use of the 
>     for statement. Please consider  the following
>     R statement:
>              for( i in 1:(len-1) )  s[i] = log(c1[i+1]/c1[i], base = exp(1) )
>     One problem I have found with this statement is that s must exist before 
>     the statement is run. Can it be written without using a for
>     loop? Would that be better?
>     
>     Thanks,
>     Bob
>     
>     ______________________________________________
>     R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>     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.
>     
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>




More information about the R-help mailing list