Return Values with R

Return Values

Concept

In R, return values are crucial as they determine what a function outputs after execution. You can specify what a function should return using the return() function, or, if omitted, R returns the last evaluated expression by default.

Syntax 

function_name <- function(arguments) {
  # Function body
  return(value)  # Specifies what to return
}

Basic Example 

# Define the function
add_numbers <- function(a, b) {
  sum <- a + b
  return(sum)
}
# Function call
result <- add_numbers(5, 3)  # 8
print(result)

Explanation:

  • The add_numbers function calculates the sum of a and b and returns it using return(sum).
  • The function call add_numbers(5, 3) returns 8.

Default Return Value

If return() is not used, R returns the last value evaluated in the function. 

# Define the function without return()
subtract_numbers <- function(a, b) {
  difference <- a - b
  difference  # The last evaluated value is returned
}
# Function call
result <- subtract_numbers(10, 4)  # 6
print(result)

Explanation:

  • Here, subtract_numbers returns the value of difference even though return() is not explicitly used.

Multiple Return Values

R functions can return multiple values by encapsulating them in a list. 

# Define the function
statistics <- function(x) {
  mean_val <- mean(x)
  sd_val <- sd(x)
  return(list(mean = mean_val, sd = sd_val))
}
# Function call
result <- statistics(c(1, 2, 3, 4, 5))
print(result)

Explanation:

  • The statistics function returns a list containing the mean and standard deviation of the input values.
  • list(mean = mean_val, sd = sd_val) creates a list with two named elements: mean and sd.

Return Value with Conditions

You can use conditional statements to determine what the function returns. 

# Define the function
categorize_number <- function(x) {
  if (x > 0) {
    return("Positive")
  } else if (x < 0) {
    return("Negative")
  } else {
    return("Zero")
  }
}
# Function calls
print(categorize_number(10))   # "Positive"
print(categorize_number(-5))   # "Negative"
print(categorize_number(0))    # "Zero"

Explanation:

  • The categorize_number function returns a different string based on whether x is positive, negative, or zero.

Implicit Return Value

If no explicit return() is used, the last evaluated expression is returned. This can simplify functions. 

# Define the function
multiply_numbers <- function(a, b) {
  a * b  # The last evaluated expression is returned
}
# Function call
result <- multiply_numbers(4, 5)  # 20
print(result)

Explanation:

  • Here, a * b is the last expression evaluated, so it is automatically returned.

Best Practices

  • Clarity: Use return() to make it clear what your function returns, especially if multiple values are computed.
  • Consistency: Be consistent in whether you use return(). Choose to use or omit it based on code readability and clarity.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print