[Rd] Negative integer subscripts in [[?

William Dunlap wdunlap at tibco.com
Mon Nov 9 22:38:26 CET 2009


Should negative subscripts be outlawed in
   x[[subscript]]
?  

Currently, if subscript is a scalar then it can only
be negative if length(x)==1 (otherwise [[ throws an
error).  If length(subscript)>1 then it gets treated
as an attempt to recursively extract an element of
a nested list.

> list(10,20)[[-1]] # get the last element out of 2
[1] 20
> list(10,20,30)[[-(1:2)]] # get the last of 3?  No.
Error in list(10, 20, 30)[[-(1:2)]] :
  attempt to select more than one element
> list(10,list(20,30))[[-c(1:2)]] # see how recursive subscripting is
done
[1] 20

If negative subscripts were not allowed in [[ then 
there might be a little less confusion about [[.

(If recursive subscripting were done by a list instead
of by an integer or character vector there might be
less confusion and it would be more flexible.)


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 



More information about the R-devel mailing list