Why is it valid to slice a vector starting with index zero?

Viewed 94
v <- 1:10

So I accidentally discovered that:

v[0:10] == v[1:10]

output:

[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

How? First of all, I thought that R indices start with 1?

1 Answers

From the R language definition (section 3.4.1, "Indexing by vectors", "Integer"):

A special case is the zero index, which has null effects: x[0] is an empty vector and otherwise including zeros among positive or negative indices has the same effect as if they were omitted.

Another SO question asks "why is this useful"?, without many satisfying answers: this is an interesting question, but seems on-topic for r-devel@r-project.org rather than SO ...

Related