0
0
R Programmingprogramming~5 mins

Variable assignment (<- and =) in R Programming

Choose your learning style9 modes available
Introduction

Variable assignment lets you store information to use later. In R, you can assign values using either <- or =.

When you want to save a number or text to use in calculations or display later.
When you need to keep track of results from a function for future use.
When you want to change or update the value stored in a variable.
When writing scripts that need to remember data between steps.
Syntax
R Programming
variable_name <- value
# or
variable_name = value

The <- operator is the traditional way to assign values in R.

The = operator also works for assignment but is often used in function arguments.

Examples
Assigns the number 10 to the variable x using the traditional arrow.
R Programming
x <- 10
Assigns the text "Alice" to the variable name using the equals sign.
R Programming
name = "Alice"
Assigns to result the value of x plus 5.
R Programming
result <- x + 5
Sample Program

This program assigns 5 to a using <-, 3 to b using =, then adds them and stores in sum. Finally, it prints the sum.

R Programming
a <- 5
b = 3
sum <- a + b
print(sum)
OutputSuccess
Important Notes

Both <- and = do the same thing for variable assignment in most cases.

Using <- is preferred by many R programmers for clarity.

Be careful not to confuse assignment (<- or =) with comparison (==).

Summary

Use <- or = to store values in variables.

<- is the traditional and clearer assignment operator in R.

Variables let you save and reuse data easily in your programs.