[R] Zero Index Origin?

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Apr 1 10:16:41 CEST 2004


I think that is an excellent illustration of my point:

> If you are writing code that works with single elements, you are
> probably a lot better off writing C code to link into R (and C is
> 0-based ...).

but even in R it is not following

> However, the R thinking is to work with whole objects (vectors, arrays,
> lists ...) and you rather rarely need to know what numbers are in an 
> index vector. 

since you can shift whole blocks at a time rather than use a while loop.

On Thu, 1 Apr 2004, Peter Wolf wrote:

> Richard A. O'Keefe wrote:
> 
> >It would be interesting to see some sample code where origin 0 is supposed
> >to make life easier, ...
> >
> An application is the implementation of algorithms which use origin 0 
> and are written in pseudo code.
> Write down the statements in R syntax, include some print or browser 
> statements and
> you are able to demonstrate the working of different approaches. Here is 
> an example for sorting
> -- I know   sort(x)   is a better solution ...
> 
> sort.6<-function(a){
>    n<-length(a)
>    adapt<-function(i){i+1}  # local function to perform the index correction
>    a<-c(0,a)
>    for(i in 2:n){
>       j<-i-1
>       a[adapt(0)]<-a[adapt(i)]
>       while(a[adapt(j)]>a[adapt(0)]){
>          a[adapt(j+1)]<-a[adapt(j)]
>          j<-j-1
>       }
>       a[adapt(j+1)]<-a[adapt(0)]
>    }
>    return(a[-1])
> }
> 
> Peter Wolf
> 
> ______________________________________________
> 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
> 
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list