Complete the code to return the sum of two numbers.
add_numbers <- function(a, b) {
return([1])
}The function should return the sum of a and b. Using a + b correctly adds the two numbers.
Complete the code to return the first element of a vector.
get_first <- function(vec) {
return(vec[1])
}In R, to get the first element of a vector, use single square brackets with index 1: vec[1].
Fix the error in the function to return the length of a vector.
vector_length <- function(x) {
len <- length(x)
return([1])
}x instead of its length.length which is a function, not a variable.The variable len stores the length of x. The function should return len.
Fill both blanks to return a named list with the vector and its mean.
summary_stats <- function(data) {
result <- list([1] = data, [2] = mean(data))
return(result)
}The list should have names values for the data vector and average for its mean.
Fill all three blanks to return a list with the sum, product, and difference of two numbers.
calculate <- function(x, y) {
result <- list([1] = x + y, [2] = x * y, [3] = x - y)
return(result)
}total instead of specific ones.The list should have names sum, product, and difference for the respective calculations.