0
0
R Programmingprogramming~10 mins

Variable assignment (<- and =) in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Variable assignment (<- and =)
Start
Assign value with <- or =
Variable stores value
Use variable later
End
Assign a value to a variable using <- or =, then the variable holds that value for later use.
Execution Sample
R Programming
x <- 5
print(x)
y = 10
print(y)
Assign 5 to x using <- and 10 to y using =, then print both variables.
Execution Table
StepCode executedVariableValueOutput
1x <- 5x5
2print(x)x55
3y = 10y10
4print(y)y1010
5End of code---
💡 All assignments done and variables printed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
xundefined555
yundefinedundefined1010
Key Moments - 2 Insights
Why can we use both <- and = for assignment in R?
Both <- and = assign values to variables in R; <- is traditional and preferred, but = works the same in most cases as shown in steps 1 and 3.
Does print(x) change the value of x?
No, print(x) only shows the value of x (step 2), it does not change x's value as seen in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of x after step 1?
A5
Bundefined
C10
D0
💡 Hint
Check the 'Value' column for variable x at step 1 in the execution_table.
At which step is the variable y assigned a value?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Code executed' column to find where y is assigned.
If we replaced y = 10 with y <- 20, what would be the output at step 4?
A10
B20
Cy
DError
💡 Hint
Variable y's value changes to 20 if assigned with <- 20, so print(y) outputs 20.
Concept Snapshot
Variable assignment in R:
Use <- or = to assign values.
Example: x <- 5 or y = 10
Both store values in variables.
Variables keep values until changed.
Use print() to see variable values.
Full Transcript
This example shows how to assign values to variables in R using two ways: the arrow <- and the equal sign =. First, x is assigned the value 5 using x <- 5. Then, printing x shows 5. Next, y is assigned 10 using y = 10. Printing y shows 10. Both assignment methods store values in variables for later use. The print function only displays the value and does not change it. The program ends after printing both variables.