0
0
R Programmingprogramming~20 mins

Code chunks and output in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Code Chunk Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple code chunk
What is the output of this R code chunk?
R Programming
x <- 5
x * 3
A[1] 53
B[1] 8
C[1] 15
DError in x * 3 : non-numeric argument to binary operator
Attempts:
2 left
💡 Hint
Remember that * means multiplication in R.
Predict Output
intermediate
2:00remaining
Output of a code chunk with a function
What is the output of this R code chunk?
R Programming
square <- function(n) {
  return(n^2)
}
square(4)
A[1] 16
B[1] 4
CError: object 'square' not found
D[1] 8
Attempts:
2 left
💡 Hint
The function returns the square of the input number.
Predict Output
advanced
2:00remaining
Output of a code chunk with vector recycling
What is the output of this R code chunk?
R Programming
v1 <- c(1, 2, 3)
v2 <- c(4, 5)
v1 + v2
AError in v1 + v2 : non-conformable arrays
B[1] 5 7 8
C[1] 5 7 3
D[1] 5 7 7
Attempts:
2 left
💡 Hint
R recycles the shorter vector to match the length of the longer vector.
Predict Output
advanced
2:00remaining
Output of a code chunk with a factor variable
What is the output of this R code chunk?
R Programming
f <- factor(c('low', 'medium', 'high', 'medium'), levels = c('low', 'medium', 'high'))
levels(f)[2] <- 'mid'
f
A
[1] low  medium high medium
Levels: low medium high
B
[1] low  mid  high mid 
Levels: low mid high
C
[1] low  mid  high mid 
Levels: low medium high
DError in levels(f)[2] <- 'mid' : replacement has length zero
Attempts:
2 left
💡 Hint
Changing the second level name changes the factor labels accordingly.
Predict Output
expert
3:00remaining
Output of a code chunk with nested functions and environment
What is the output of this R code chunk?
R Programming
outer <- function(x) {
  inner <- function(y) {
    x + y
  }
  inner(10)
}
outer(5)
A[1] 15
B[1] 5
CError: object 'y' not found
D[1] 10
Attempts:
2 left
💡 Hint
The inner function uses x from the outer function's environment.