Challenge - 5 Problems
R Assignment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of variable assignment with <- and =
What is the output of the following R code?
x <- 5 y = 10 z <- x + y print(z)
R Programming
x <- 5 y = 10 z <- x + y print(z)
Attempts:
2 left
💡 Hint
Remember that both <- and = assign values to variables in R.
✗ Incorrect
Both <- and = assign values to variables in R. Here, x is 5, y is 10, so z is 15.
❓ Predict Output
intermediate2:00remaining
Difference between <- and = in function arguments
What will this R code print?
f <- function(a = 3) { a * 2 }
f()
f <- function(a <- 3) { a * 2 }
f()R Programming
f <- function(a = 3) { a * 2 } f() f <- function(a <- 3) { a * 2 } f()
Attempts:
2 left
💡 Hint
Check how default arguments are assigned in R functions.
✗ Incorrect
In R, default arguments use =, not <-. Using <- in function arguments causes an error.
🔧 Debug
advanced2:00remaining
Why does this assignment fail?
Consider this R code:
Why does this cause an error?
5 <- x
Why does this cause an error?
R Programming
5 <- xAttempts:
2 left
💡 Hint
Think about what can be on the left side of an assignment.
✗ Incorrect
In R, the left side of <- must be a variable name, not a constant like 5.
❓ Predict Output
advanced2:00remaining
Output of chained assignments
What is the output of this R code?
a <- b <- 7 print(a) print(b)
R Programming
a <- b <- 7 print(a) print(b)
Attempts:
2 left
💡 Hint
Remember that assignment in R returns the assigned value.
✗ Incorrect
The expression b <- 7 assigns 7 to b and returns 7, which is then assigned to a. Both a and b become 7.
📝 Syntax
expert2:00remaining
Which assignment causes a syntax error?
Which of these R assignments will cause a syntax error?
Attempts:
2 left
💡 Hint
Look carefully at the position of the assignment operator.
✗ Incorrect
The assignment operator <- must be between variable and value. '<- z 30' is invalid syntax.