Menu
Coddy logo textTech

Assignment Operators

Part of the Fundamentals section of Coddy's R journey — lesson 14 of 78.

You've already been using the assignment operator <- to store values in variables. R actually provides multiple ways to assign values, and understanding them helps you write cleaner code.

The most common assignment operator in R is the left arrow <-:

score <- 100
name <- "Alice"

R also supports the equals sign = for assignment, which works the same way:

score = 100
name = "Alice"

While both work, the R community strongly prefers <- as it makes the direction of assignment clear and avoids confusion with the = used for function arguments.

R also has a right arrow -> that assigns in the opposite direction, placing the value on the left and the variable name on the right:

50 -> age

This is rarely used in practice, but it's valid R syntax. Stick with <- for consistency and readability in your code.

challenge icon

Challenge

Easy

Practice using different assignment operators to store values in variables.

Complete the following tasks using the specified assignment operators:

  1. Create a variable called temperature and assign it the value 72 using the left arrow operator <-
  2. Create a variable called humidity and assign it the value 45 using the equals sign =
  3. Assign the value 30 to a variable called wind_speed using the right arrow operator ->

After creating all three variables, use the print() function to display temperature, humidity, and wind_speed in that exact order.

Cheat sheet

R provides multiple assignment operators to store values in variables:

The left arrow <- is the most common and preferred assignment operator:

score <- 100
name <- "Alice"

The equals sign = also works for assignment:

score = 100
name = "Alice"

The R community prefers <- as it makes the direction of assignment clear and avoids confusion with = used for function arguments.

The right arrow -> assigns in the opposite direction (value on left, variable on right):

50 -> age

This is rarely used but valid syntax. Stick with <- for consistency and readability.

Try it yourself

# TODO: Write your code below

# 1. Create 'temperature' variable with value 72 using <- operator

# 2. Create 'humidity' variable with value 45 using = operator

# 3. Create 'wind_speed' variable with value 30 using -> operator

# Print all three variables in order: temperature, humidity, wind_speed
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals