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 isdone [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
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)==1Actually, 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@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >[[alternative HTML version deleted]]
On 09/11/2009 4:38 PM, 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 (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 [[.I agree, it would be better not to allow negatives here, but as John said it's probably too late to do away with them.> (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.)I don't follow this. Recursive lists are trees, and you specify a single element of a tree by specifying a sequence of indices. Why would it be less confusing to give a list? What extra flexibility could there be? I suppose you could mix integer and character indices, but what would be meant by x[[ list(1, list(2,3), 4) ]] ? I don't know the original motivation for allowing vector indexing to lists, but I extended it to pairlists so that it would be possible to specify a location within a function exactly, by walking down the parse tree. I think it's something that would be rarely used, but when you need it, it's very handy. Duncan Murdoch
Maybe Matching Threads
- Simultaneous subscripts and superscripts
- handling of zero and negative indices in src/main/subscript.c:mat2indsub() (PR#7824)
- memory misuse in subscript code when rep() is called in odd way
- Help: formatting the result of 'cut' function
- Error in sort(abs(diff(genomdat)))[1:n.keep] : only 0's may be mixed with negative subscripts