what is the difference between <- and = in r

In R, both the equal sign "=" and the left arrow "<-" can be used as assignment operators to assign values to variables. However, there is a subtle difference between the two operators.

The equal sign "=" is used for simple assignments and is often used to assign a constant value to a variable. For example, the following code assigns the value 5 to the variable "x":

main.r
x = 5
6 chars
2 lines

On the other hand, the left arrow "<-" is more commonly used for complex assignments and is often used to assign the result of a computation or function call to a variable. For example, the following code assigns the sum of the values 2 and 3 to the variable "x":

main.r
x <- 2 + 3
11 chars
2 lines

Both the equal sign "=" and the left arrow "<-" can be used interchangeably in most cases. However, some R programmers prefer to use the left arrow "<-" for assignments to make the code more readable and distinguish assignments from logical operators.

gistlibby LogSnag