Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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?

like image 907
Big Cat Public Safety Act Avatar asked Dec 19 '18 15:12

Big Cat Public Safety Act


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 [email protected] rather than SO ...

like image 150
Ben Bolker Avatar answered Nov 11 '22 11:11

Ben Bolker