[R] indexing a vector starting from 0

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun Jul 24 22:50:22 CEST 2005


Federico Calboli <f.calboli at imperial.ac.uk> writes:

> Hi All,
> 
> I would like to ask if it possible to start indexing a vector from 0:
> 
> x = 1:10
> 
> y = c(0,0,3,4,5,6,0,8,9,10)
> 
> I need to use y as an index to extract the values of x, BUT I cannot  
> cull/transform the 0s. What I would like is to start counting the  
> elements of x 0:9 rather than 1:10. Would this be at all possible?

There's a 0array package on CRAN, but why not just add 1 to the index?

> x[y+1]
 [1]  1  1  4  5  6  7  1  9 10 NA

In general, 0-based indexing is possible, but messes with other things
in R, most obviously negative indexes, but also constructions that use
match(...., nomatch=0)

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list