Skip to content
Prev 206416 / 398506 Next

advice/opinion on "<-" vs "=" in teaching R

Barry Rowlingson wrote:
The most common use I see that I like is within a conditional test like

if (  !is.null(x <- get("x", somehow)) && length(x) == 1) { dosomething }

The x variable is only used for the test, but since it is used twice 
there, the assignment saves getting it twice.  You could expand it to 
two lines

x <- get("x", somehow)
if ( !is.null(x) && length(x) == 1) { dosomething }

but I find that a tiny bit harder to read. 

On the other hand, I would never use the examples you gave, because I'd 
have no idea what the value of x would be, since it depends on the order 
of evaluation of the arguments.  In R, I don't even know for sure if the 
assignment would be evaluated at all, let alone before the x argument.

Duncan Murdoch