Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

R Assignment Operators


Share

Assignment Operators

Assignment operators are used to store values in variables.

You already know the most common one from the Variables chapter: <- stores the value on the right in the variable on the left:

Example

x <- 10

x
Try it Yourself »

Other Ways to Assign

R has two more ways to assign a value. = works like <-, and -> assigns from left to right:

Example

x <- 5
y = 10
15 -> z

x
y
z
Try it Yourself »

A list of the assignment operators:

Operator Example Description
<- x <- 5 Stores 5 in x (the most common way in R)
= x = 5 Same as x <- 5
-> 5 -> x Same as x <- 5, but written from left to right
<<- x <<- 5 Global assignment, used inside functions

Tip: Most R programmers use <-. Use it in your own code too, so it is easy to read for others.

You will learn more about <<- in the Global Variables chapter.



Change the Value of a Variable

Many programming languages have shorthand operators like += to add a value to a variable. R does not have them.

Instead, use the variable on both sides of <-:

Example

score <- 10
score <- score + 5

score
Try it Yourself »

R first calculates score + 5 (which is 15), and then stores the result back in score.


Real-Life Example: Counting People

Imagine a program that counts how many people are in a room. Add 1 when someone enters, and subtract 1 when someone leaves:

Example

people <- 0

people <- people + 1
people <- people + 1
people <- people + 1
people <- people - 1

people
Try it Yourself »

Three people entered and one person left, so there are 2 people in the room.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.