We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Dr.Bonobo • 8 years ago

Thanks!
So I get that there's a difference when using <- instead of = within a formula to assign parameters. But is there any case where:
new.var <- other.var
will have a different output that
new.var = other.var ?

Bob • 7 years ago

It should be the same generally but it's probably a safer bet to use new.var <- other.var in general.

Some times using '=' causes issue for example:

System.time(
new.var <- other.var
)
this function works

System.time(
new.var = other.var
)
This function does not work. This probably has to do with how the '=' makes it evaluate the function internally, so it just passes a stored value to system.time, whereas '<-' keeps time for the whole calculation.

Guest • 7 years ago

No it's because in a function call you can't assign an object with = because = means assigning arguments there.

When you write

system.time(new.var = other.var)

it means "call function system.time, setting the argument new.var to other.var" and so you should get an unused argument error, as the argument 'new.var' is not used by the function system.time.

Whereas when you write

system.time(new.var <- other.var)

it means "assign other.var to new.var, and then call system.time with it's first argument (which in this case is expr) set to new.var <- other.var" and is equivalent to writing system.time(expr = new.var <- other.var).

Also it's worth noting that system.time((new.var=other.var)) is equivalent to system.time(new.var <- other.var). So if you have an R expression that you want to time you don't have to change = to <- you can just wrap the whole expression in an extra pair of parentheses.

Peter • 7 years ago

Who came up with this operator? All these years, Python managed to get by with just = . Talking about readability , = is better on the eyes, no?. and one key stroke vs. two, even given R studio shortcut.

Gabe Coker • 6 years ago

I don't know. I'm just beginning to work with R, and coming from BASIC and TurboPascal, I kinda like having the three options <-, ->, and = for logical visual flow. Maybe I'm old-fashioned.

Nikhil Muthukrishnan • 7 years ago

I'v only seen the double assignment operator of baser in use and hoping beyond hope it could be tied in to reactivity for auto updating documents,