📖 Scenario: You are learning how to store information in R using variables. Variables are like labeled boxes where you keep values. In R, you can assign values to variables using two common symbols: <- and =.Imagine you want to keep track of the number of apples you have. You can put that number in a box called apples using either <- or =.
🎯 Goal: You will create two variables called apples and oranges. You will assign the number 10 to apples using <- and the number 5 to oranges using =. Then you will print both variables to see their values.
📋 What You'll Learn
Create a variable called apples and assign it the value 10 using <-
Create a variable called oranges and assign it the value 5 using =
Print the value of apples
Print the value of oranges
💡 Why This Matters
🌍 Real World
Variables let you store data like counts, names, or measurements so your program can use them later.
💼 Career
Understanding variable assignment is a basic skill needed for all programming jobs, including data analysis and software development.
Progress0 / 4 steps
1
Create variable apples with value 10 using <-
Create a variable called apples and assign it the value 10 using the assignment operator <-.
R Programming
Hint
Use apples <- 10 to assign 10 to the variable apples.
2
Create variable oranges with value 5 using =
Create a variable called oranges and assign it the value 5 using the assignment operator =.
R Programming
Hint
Use oranges = 5 to assign 5 to the variable oranges.
3
Print the value of apples
Write a line of code to print the value of the variable apples using the print() function.
R Programming
Hint
Use print(apples) to show the value of apples.
4
Print the value of oranges
Write a line of code to print the value of the variable oranges using the print() function.
R Programming
Hint
Use print(oranges) to show the value of oranges.
Practice
(1/5)
1. In R, which operator is traditionally used to assign a value to a variable?
easy
A. :=
B. ==
C. ->
D. <-
Solution
Step 1: Understand assignment operators in R
R uses <- and = for assignment, but <- is traditional.
Step 2: Identify the traditional assignment operator
The operator <- is the classic and clearer way to assign values.
Final Answer:
<- -> Option D
Quick Check:
Traditional assignment = <- [OK]
Hint: Remember: use <- for clear assignment in R [OK]
Common Mistakes:
Confusing == (comparison) with assignment
Using -> which assigns in reverse
Thinking := is valid in R
2. Which of the following is a correct way to assign the number 10 to variable x in R?
easy
A. x <- 10
B. 10 -> x <-
C. x == 10
D. x ==
Solution
Step 1: Check each option for correct assignment syntax
x == 10 uses == which is comparison, not assignment. 10 -> x <- has invalid syntax. x == is incomplete.
Step 2: Confirm valid assignment
x <- 10 uses <- correctly to assign 10 to x.
Final Answer:
x <- 10 -> Option A
Quick Check:
Correct assignment uses <- or = [OK]
Hint: Use <- or = for assignment, not == [OK]
Common Mistakes:
Using == instead of <- or =
Writing incomplete assignment
Mixing operators incorrectly
3. What will be the output of this R code?
x <- 5
y = 3
z <- x + y
print(z)
medium
A. 8
B. 53
C. Error
D. NULL
Solution
Step 1: Understand variable assignments
x is assigned 5 using <-, y is assigned 3 using =, both valid assignments.
Step 2: Calculate z and print
z is assigned x + y = 5 + 3 = 8, so print(z) outputs 8.
Final Answer:
8 -> Option A
Quick Check:
5 + 3 = 8 [OK]
Hint: Add assigned values to find output [OK]
Common Mistakes:
Concatenating numbers as strings
Expecting syntax error from mixing <- and =
Ignoring print output
4. Identify the error in this R code:
value <- 10
value =+ 5
print(value)
medium
A. Runtime error on print
B. value becomes 15 correctly
C. Syntax error due to =+ operator
D. value remains 10
Solution
Step 1: Analyze the assignment statements
First line assigns 10 to value correctly. Second line uses =+ which is invalid syntax in R.
Step 2: Understand the error caused
=+ is not a valid operator; it causes a syntax error before print runs.
Final Answer:
Syntax error due to =+ operator -> Option C
Quick Check:
=+ is invalid syntax [OK]
Hint: Use <- or =, not =+ for assignment [OK]
Common Mistakes:
Typing =+ instead of += (which R does not support)
Expecting += to work like other languages
Ignoring syntax errors
5. You want to swap the values of two variables a and b in R. Which code correctly does this using assignment operators?
hard
A.
a = b
b = a
B.
temp <- a
a <- b
b <- temp
C.
a <- b
b <- a
D.
a =+ b
b =- a
Solution
Step 1: Understand swapping logic
Swapping requires a temporary variable to hold one value to avoid overwriting.
Step 2: Check each option for correct swap
temp <- a
a <- b
b <- temp
uses temp to hold a, then assigns b to a, then temp to b, correctly swapping values. Options B and C overwrite without temp, losing original values.
a =+ b
b =- a
has invalid syntax.
Final Answer:
temp <- a; a <- b; b <- temp -> Option B
Quick Check:
Use temp variable to swap [OK]
Hint: Use temp variable to swap values safely [OK]
Common Mistakes:
Overwriting variables without temp
Using invalid operators like =+
Assuming simultaneous assignment swaps
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.