Alex Rivera | Logout

Why do vector indices in R start with 1, instead of 0?

Asked 2010-06-28T19:02:36.880
56

What is the reason that vector indices in R start with 1, instead of the usual 0?

Example:

> arr<-c(10,20)
> arr[0]
numeric(0)
> arr[1]
[1] 10
> arr[2]
[1] 20

Is it just that they want to store extra information about the vector and didn't know where to store it except as the vector's first element?

Edit
Report

1 Answer

11

0 is only "usual" because that's what C did, and a lot of later languages slavishly copied C syntax. By default in Fortran arrays are 1-based.

In Ada there is no default and you have to pick the beginnning and end ranges. Interestingly, it seems that most code I've come across picks '1' for the lower bound. I think that's a pretty good indication of where folks would have gone given a free choice.

answered 2010-06-28T19:19:06.963

Your Answer