[R] Zero Index Origin?

Peter Wolf s-plus at wiwi.uni-bielefeld.de
Thu Apr 1 09:58:46 CEST 2004


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




More information about the R-help mailing list