0
0
R Programmingprogramming~20 mins

Running R code in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
R Code Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple R loop
What is the output of the following R code?
R Programming
result <- c()
for(i in 1:4) {
  result <- c(result, i^2)
}
print(result)
A[1] 1 2 3 4
B[1] 1 8 27 64
C[1] 2 4 6 8
D[1] 1 4 9 16
Attempts:
2 left
💡 Hint
Think about what i^2 means inside the loop.
🧠 Conceptual
intermediate
2:00remaining
Understanding R function return values
What will be the output when running this R code?
R Programming
my_func <- function(x) {
  y <- x + 2
  z <- y * 3
  z
  y
}
print(my_func(4))
A[1] 6
B[1] 18
C[1] 12
D[1] 0
Attempts:
2 left
💡 Hint
Remember that R returns the last evaluated expression in a function.
🔧 Debug
advanced
2:00remaining
Identify the error in R code
What error does this R code produce when run?
R Programming
x <- c(1, 2, 3)
print(x[4])
AError: subscript out of bounds
BNA (no error, prints NA)
CNULL
DSyntaxError
Attempts:
2 left
💡 Hint
What happens when you access an index outside the vector length in R?
Predict Output
advanced
2:00remaining
Output of nested if-else in R
What is the output of this R code?
R Programming
x <- 5
if (x > 3) {
  if (x < 10) {
    print("Between 4 and 9")
  } else {
    print("10 or more")
  }
} else {
  print("3 or less")
}
A"Between 4 and 9"
B"10 or more"
CError: unexpected symbol
D"3 or less"
Attempts:
2 left
💡 Hint
Check the conditions carefully and their order.
🚀 Application
expert
2:00remaining
Calculate the sum of even numbers using R code
Which option correctly calculates the sum of even numbers from 1 to 10 in R?
Asum(c(2,4,6,8,10))
Bsum(1:10 %% 2 == 0)
Csum(seq(2, 10, by=2))
Dsum(1:10[1:10 %% 2 == 0])
Attempts:
2 left
💡 Hint
Use seq() to generate even numbers or filter with modulo operator.