Search code examples
rperformanceassignment-operator

R: Is there any performance difference between using = or <-?


For example:

a <- c(1,2,3) 

or

a = c(1,2,3).

Obviously for small data frames or lists, it won't matter at all. I am thinking more about big datasets or using them inside a for-loop.


Solution

  • No measurable performance difference; more about readability and convention. It's all the computations that R is doing that will make or break you.

    Use <-; "R In Action" says on page 7 that it's standard and other R developers will make fun of you if you don't.

    There might be a deeper reason in the R documentation:

    There are three different assignment operators: two of them have leftwards and rightwards forms.

    The operators <- and = assign into the environment in which they are evaluated.

    The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

    It's usually a good idea to use the proper idiom of the language you're writing in.