[R] questions on some operators in R

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jun 19 00:26:23 CEST 2010


On Fri, Jun 18, 2010 at 2:15 PM, Horace Tso <Horace.Tso at pgn.com> wrote:
> Li li,
>
> I know many S-language old timers would tell you to use <- over = for assignment. Speaking from my own painful experience of debugging S/R codes, I much much much prefer '='. In fact, I'd like to see the R language get ride of '<-' as the assignment operator.
>
> Here is why.
>
>> x = -5:10
>> x
>  [1] -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  8  9 10
>
> Now I want to find elements of x which are smaller than negative 2, or -2. So naturally I'd do,
>
>> which(x<-2)
> Error in which(x <- 2) : argument to 'which' is not logical
>
> Oops, what happened? If you look up help pages for 'which', you'd find no clue.
>
> What occurred in the parenthesis is that you've overidden your vector x with a single value of 2, thanks to the assignment operator '<-'.
>
> This' a big problem not just because you might end up spending hours finding out what's wrong with such innocent expression. The worst part is, you'd have lost your vector x forever. Just image if x is 1200 by 1200 matrix.
>

I prefer <- because it makes more sense.  It evokes the idea of
assignment, which is what it is, whereas = reminds one of equality
which is wrong since its not equality but assignment.

Its also consistent with <<- whereas it seems highly inconsistent to
use = and <<- .   Its also less ambiguous when not used on the left
hand side of a statement.



More information about the R-help mailing list