0
0
R Programmingprogramming~5 mins

Assignment operators in R Programming

Choose your learning style9 modes available
Introduction

Assignment operators let you store values in variables so you can use them later.

When you want to save a number or text to use it again.
When you need to update a value step by step.
When you want to keep results from calculations.
When you want to give a name to a value for easier reading.
When you want to change a variable based on its current value.
Syntax
R Programming
variable <- value
variable = value
value -> variable

<- is the most common assignment operator in R.

You can also use = for assignment, but <- is preferred for clarity.

Examples
Assigns the number 5 to the variable x.
R Programming
x <- 5
Assigns the text "Alice" to the variable name using =.
R Programming
name = "Alice"
Assigns the number 10 to the variable y using the rightward operator.
R Programming
10 -> y
Sample Program

This program assigns 10 to a, 20 to b, adds them, and stores the result in sum. Then it prints the sum.

R Programming
a <- 10
b <- 20
sum <- a + b
print(sum)
OutputSuccess
Important Notes

Use <- for assignment to follow R community style.

Assignment operators do not print the value unless you explicitly print it.

You can assign any type of data, like numbers, text, or even results of functions.

Summary

Assignment operators store values in variables.

<- is the preferred operator in R.

Variables help you reuse and update data easily.