0
0
R Programmingprogramming~20 mins

Assignment operators in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Assignment Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of compound assignment with addition
What is the output of the following R code?
R Programming
x <- 5
x += 3
print(x)
AError: unexpected '=' in "x +="
B[1] 5
C[1] 3
D[1] 8
Attempts:
2 left
💡 Hint
R does not support the '+=' operator like some other languages.
Predict Output
intermediate
2:00remaining
Correct way to add and assign in R
Which option correctly adds 3 to variable x and assigns the result back to x in R?
R Programming
x <- 5
# Add 3 to x and assign back
Ax =+ 3
Bx += 3
Cx <- x + 3
Dx <-+ 3
Attempts:
2 left
💡 Hint
Use the standard assignment operator with an expression.
Predict Output
advanced
2:00remaining
Output of chained assignment
What is the output of this R code?
R Programming
a <- b <- 10
print(a)
print(b)
AError: invalid syntax
B
[1] 10
Error: object 'b' not found
C
[1] 20
[1] 10
D
[1] 10
[1] 10
Attempts:
2 left
💡 Hint
In R, assignment can be chained from right to left.
Predict Output
advanced
2:00remaining
Output of assignment with global environment
What is the output of this R code?
R Programming
x <- 5
f <- function() {
  x <<- 10
  print(x)
}
f()
print(x)
A
[1] 10
[1] 5
B
[1] 10
[1] 10
C
[1] 5
[1] 5
DError: object 'x' not found
Attempts:
2 left
💡 Hint
The '<<-' operator assigns to the variable in the parent environment.
🧠 Conceptual
expert
3:00remaining
Behavior of assignment operators in R environments
Which statement best describes the difference between '<-' and '<<-' assignment operators in R?
A'<-' assigns a value in the current environment; '<<-' assigns a value in the parent environment or higher if not found.
B'<-' assigns a value in the current environment; '<<-' assigns a value in the global environment only.
C'<-' assigns a value globally; '<<-' assigns a value locally within a function.
D'<-' and '<<-' are interchangeable and have no difference in scope.
Attempts:
2 left
💡 Hint
Think about how R searches environments when assigning with '<<-'.