0
0
R Programmingprogramming~10 mins

Return values in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return the sum of two numbers.

R Programming
add_numbers <- function(a, b) {
  return([1])
}
Drag options to blanks, or click blank then click option'
Aa * b
Ba - b
Ca + b
Da / b
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction (-) instead of addition (+).
Forgetting to use return() to send back the result.
2fill in blank
medium

Complete the code to return the first element of a vector.

R Programming
get_first <- function(vec) {
  return(vec[1])
}
Drag options to blanks, or click blank then click option'
A[[2]]
B[1]
C[[1]]
D[2]
Attempts:
3 left
💡 Hint
Common Mistakes
Using double square brackets ([[1]]) which is for lists, not vectors.
Using index 2 instead of 1.
3fill in blank
hard

Fix the error in the function to return the length of a vector.

R Programming
vector_length <- function(x) {
  len <- length(x)
  return([1])
}
Drag options to blanks, or click blank then click option'
Ax
Bx.length
Clength
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the vector x instead of its length.
Trying to return length which is a function, not a variable.
4fill in blank
hard

Fill both blanks to return a named list with the vector and its mean.

R Programming
summary_stats <- function(data) {
  result <- list([1] = data, [2] = mean(data))
  return(result)
}
Drag options to blanks, or click blank then click option'
Avalues
Baverage
Cmean
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for both list elements.
Using incorrect or unclear names.
5fill in blank
hard

Fill all three blanks to return a list with the sum, product, and difference of two numbers.

R Programming
calculate <- function(x, y) {
  result <- list([1] = x + y, [2] = x * y, [3] = x - y)
  return(result)
}
Drag options to blanks, or click blank then click option'
Asum
Bproduct
Cdifference
Dtotal
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the names and operations.
Using generic names like total instead of specific ones.