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?
The unusual thing about R vectors is that the first element (the first number in the set) is referenced by '[1]', whereas with most sorts of arrays the first element is referenced with '[0]'. For me, the reason for the difference is implied by the term itself: 'vector'.
In R, the indexing begins from 1. While NA and zero values are allowed as indexes, rows of an index matrix containing a zero are ignored, whereas rows containing an NA produce an NA in the result.
R is a "platform for experimentation and research". Its aim is to enable "statisticians to use the full capabilities of such an environment" without rethinking the way they usually deal with statistics. So people use formulas to make regression models, and people start counting at 1.
Vector is just an array, so this is 0-indexed. Of course you can store a dummy data on position 0, but I recommend you to subtract 1 from the position you desire. Or you can simply create the desired class that can access the desired element by the index which starts by 1.
FORTRAN is one language that starts arrays at 1. Mathematicians deal with vectors that always start with component 1 and go through N. Linear algebra conventions start with row and column numbered 1 and go through N as well.
C started with zero because of the pointer arithmetic that was implicit underneath. Java, JavaScript, C++, and C# followed suit from C.
Vectors in math are often represented as n-tuples, elements of which are indexed from 1 to n. I suspect that r wanted to stay true to this notation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With