Re: [Rd] Negative integer subscripts in [[?

From: John Chambers <jmc_at_r-project.org>
Date: Mon, 09 Nov 2009 14:00:17 -0800

Hmm. It's something that is worth discouraging, because the user probably didn't mean it.

But it is more or less consistent with the language, and prohibiting it at this date is unlikely to be worth the possible breakage (I know, who could possibly rely on it? But with a few thousand packages, somebody likely does.)

John

William Dunlap wrote:
> Should negative subscripts be outlawed in
> x[[subscript]]
> ?
>
> Currently, if subscript is a scalar then it can only
> be negative if length(x)==1
Actually, length(x)==2 as in your example.
> (otherwise [[ throws an
> error). If length(subscript)>1 then it gets treated
> as an attempt to recursively extract an element of
> a nested list.
>

Yes, but the interpretation is essentially consistent with multiple elements and positive values:

 > xx = list(c(1,2),c(3,4))
 > xx[[-1]]
[1] 3 4
 > xx[[-2]]
[1] 1 2
 > xx[[c(-1,-2)]]
[1] 3
 > xx[[c(2,1)]]
[1] 3

>
>> 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
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

        [[alternative HTML version deleted]]



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 09 Nov 2009 - 22:04:28 GMT

This archive was generated by hypermail 2.2.0 : Tue 10 Nov 2009 - 00:30:22 GMT