[R] The end of Matlab

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Fri Dec 12 16:56:30 CET 2008

Claudia Beleites wrote:
>>> Wacek:
>>>> x[3:]
>>>> instead of
>>>> x[3:length(x)]
>>>> x[3:end]
>>> I don't think that would help:
>>> what to use for end - 3 within the convention that negative values mean
>>> exclusion?
>> might seem tricky, but not impossible:
>> x[-2]
>> # could mean 'all except for 2nd', as it is now
>> x[1:-2]
>> # could mean 'from start to the 2nd backwards from the end'
> I know you get thus far. You might even think to decide whether exclusion or 
> 'from the end' is meant from ascending ./. descending order of the sequence, 
> but this messes around with returning the reverse order.

that's a design issue.  one simple solution is to have this sort of
indexing return always in ascending order.  thus,

x = 1:5
# 1 2 3 4 5
# NULL rather than 5 4 3 2 1 -- as in matlab or python

# 5 4 3 2 1

that is, the ':'-based indexing can be made not to mess with the order. 
for reversing the order, why not use:

# 5 4 3 2 1

# 3 2 1 rather than x[c(-3,-4,-5)], which would be 1 2

>> since r disallows mixing positive and negative indexing, the above would
>> not be ambiguous.  worse with
>> x[-3:-1]
>> which could mean both 'except for 3rd, 2nd, and 1st' and 'from the 3rd
>> to the 1st from the end', and so would be ambiguous.  in this context,
>> indeed, having explicit 'end' could help avoid the ambiguity.
> that's the problem.
> also: how would 'except from the 5th last to the 3rd last' be expressed?

for exclusions you'd need to use negative indices anyway:


now, neither x[-5:-3] nor x[-3:-5] would do the job they do now, but the
above is not particularly longer, while selecting the
5th-to3rd-from-the-end columns is simply x[-5:-3] (which could be made
to fail on out-of-range indices) instead of something like x[length(x) -
4:2] (which will silently do the wrong thing if length(x) < 4, and thus
requires extra care).

this is a rather loose idea, and unrealistic in the context of r, but i
do not see much problem with it on the conceptual level.


More information about the R-help mailing list