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 -> ageThis is rarely used in practice, but it's valid R syntax. Stick with <- for consistency and readability in your code.
Challenge
EasyPractice using different assignment operators to store values in variables.
Complete the following tasks using the specified assignment operators:
- Create a variable called
temperatureand assign it the value72using the left arrow operator<- - Create a variable called
humidityand assign it the value45using the equals sign= - Assign the value
30to a variable calledwind_speedusing 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 -> ageThis 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_speedThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values