Skip to content
Prev 396029 / 398500 Next

an issue about subsetting a vector

As with a lot of things in R, the behaviour is counterintuitive but 
(arguably) logical. The key (maybe) is that `a[-x]` does not mean "take 
all of the elements of a except those indicated by `x`, but rather, 
"negate x, then take all but the negative elements".  In other words,

  -integer(0)  is   integer(0)  (we multiply *each of the elements of 
integer(0)* by -1, but integer(0) has no elements)

   that reduces to a[integer(0)]

and from there you get "select none of the elements".

   A related point is explained in the R Inferno 
https://www.burns-stat.com/pages/Tutor/R_inferno.pdf section 8.1.13, 
"negative nothing is something".

   See also

https://stackoverflow.com/questions/42615728/subsetting-vector-how-to-programatically-pass-negative-index-safely

https://stackoverflow.com/questions/40026975/subsetting-with-negative-indices-best-practices/40029485#40029485
On 2024-03-24 2:19 p.m., Paulo Barata wrote: