[R] The end of Matlab

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Fri Dec 12 15:34:23 CET 2008


Claudia Beleites wrote:
> Am Freitag 12 Dezember 2008 13:10:20 schrieb Patrick Burns:
>   
>> How about:
>>
>> x[, -seq(to=ncol(x), length=n)]
>>     
> Doing it is not my problem. I just agree with Mike in that I would like if I 
> could do shorter than: 
>
> x[, 1 : (ncol(x) - n)] 
>
> which I btw prefer to your solution.
>
> Also, I don't have a problem writing generalized versions of head and tail to 
> work along other/more dimensions. Or a combined function, taking first and last 
> arguments. 
>
> Still, they would not be as convenient to use as matlab's:
> 3 : end - 4
> which btw. also does not need parentheses.
>
> I guess the general problem is that there is only one thing with integers that 
> can easily be (ab)used as a flag: the negative sign. 
>
> But there are (at least) 2 possibly useful special ways of indexing: 
> - exclusion (as in R)
> - using -n for end - n (as in perl)
>
> Now we enjoy having a shortcut for exclusion (at least I do), but still feel 
> that marking "from the end" would be useful.
>
> As no other signs (in the sense of flag) are available for integers, we won't 
> be able to stop typing somewhat more in R.
>
> 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'

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.


vQ



More information about the R-help mailing list