Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

r

vector

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?

like image 395
Frank Avatar asked Jun 28 '10 19:06

Frank


People also ask

Why does R start at 1 not 0?

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'.

Do R indexes start at 0 or 1?

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.

Why does R count from 1?

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.

Do Vectors start at 0?

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.


2 Answers

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.

like image 130
duffymo Avatar answered Sep 30 '22 21:09

duffymo


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.

like image 26
Jan Gorzny Avatar answered Sep 30 '22 21:09

Jan Gorzny