[R] Zero Index Origin?
Barry Rowlingson
B.Rowlingson at lancaster.ac.uk
Wed Mar 31 13:36:48 CEST 2004
Bob Cain wrote:
> At
> this early stage of my learning I can't yet determine if there is a way
> to effect what in APL was zero index origin, the ordinality of indexes
> starts with 0 instead of 1. Is it possible to effect that in R without
> a lot of difficulty?
>
Clearly R wasn't written by Dijkstra:
http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF
This text was pointed out to me when I started using Python, which has
zero-based indexing. Python can look so much like R, but there are
subtle differences.
R:
> x=c(5,4,3,2,1)
> x[3]
[1] 3
> x[2:4]
[1] 4 3 2
compare:
Python:
>>> x=[5,4,3,2,1]
>>> x[3]
2
>>> x
[5, 4, 3, 2, 1]
>>> x[2:4]
[3, 2]
A single element from a sequence in python is indexed from zero, hence
x[3] == 2, but a range indexes from the commas between the limits of the
range. Hence x[2:4] is the elements between comma 2 and comma 4 - hence
its only 2 elements.
Did my head in when I first started pythoning. Flipping between R and
python is not recommended, kudos to all those involved in such R-python
links...
Baz
More information about the R-help
mailing list