Functions#

This lesson will cover how R allows you to create functions, just as we learned in Python.

Syntax#

The syntax for defining a function in R is as follows:

fname <- function (param1, param2, ... ) {
    # logic goes here
    # return(value)
}

This will create a function named fname that takes param1, param2, and so on as parameters.

When the function is called, R executes the statements inside the curly braces and then outputs the contents of the value variable to the user.

As in Python, only the value within the parentheses in the return function is returned. Any variables defined within the function will not be accessible outside of it after the function completes.

Here is an example:

var1 = 8
var2 = 16

this_func <- function(var1, var2) {
  res = 2*var1 + var2
  return(res)
}

this_func(var1, var2)
32

Now, look at the following example:

this_func <- function(val1,val2) {
  # perform the math
  res = 2*val1 + val2
  # return the result
  res
}

# pass values to the function
this_func(8,16)
32
Info: In contrast to Python, R functions implicitly return the value of the last evaluated expression, so an explicit return statement is not required. However, it might be used for clarity, particularly if you want to exit the function early.

Keyword arguments#

Like Python, R also supports the use of keyword arguments, allowing you to specify arguments in any order when calling the function.

this_func(val2=25, val1=15)
55
# Beware that this behavior would differ if you were to pass these values as positional arguments.
this_func(25, 15)
65

Default Values#

Like in Python, functions in R can also include paramaters that take default values.

Beware: Default values in R functions are assigned through the = operator, not the assignment operator (->).
div_these <- function(var1, var2 = 2) {
  newval = var1 / var2
  newval
}
# pass in both arguments
div_these(20, 5)
4
# pass in only the first argument
div_these(10)
5
# Again, we can pass in arguments by their name too 
div_these(var2 = 5, var1 = 20)
4

Built-in functions#

Since R was originally designed as a programming language for statistical computations, its base installation includes a generous number of mathematical functions. In Python, most of these need to be imported from NumPy.

Here is a simplied list of all these functions. Refer to the documentation for a more detailed list.

  • Basic Arithmetic

    • abs(x): Absolute value.

    • sqrt(x): Square root of.

    • exp(x): Exponential of (e^x).

    • log(x, base = exp(1)): Logarithm (default is natural log; specify base for other bases). (In Python: np.log)

    • log10(x): Base-10 logarithm.

    • log2(x): Base-2 logarithm.

    • factorial(x): Factorial.

    • pow(x, y): Alternative to x ^ y.

  • Trigonometric Functions

    • sin(x): Sine .

    • cos(x): Cosine.

    • tan(x): Tangent.

    • asin(x): Inverse sine (arcsin).

    • acos(x): Inverse cosine (arccos).

    • atan(x): Inverse tangent (arctan).

    • sinh(x), cosh(x), tanh(x): Hyperbolic sine, cosine, and tangent.

    • asinh(x), acosh(x), atanh(x): Inverse hyperbolic sine, cosine, and tangent.

  • Rounding Functions

    • round(x, digits = 0): Rounds to the specified number of decimal places.

    • floor(x): Rounds down to the nearest integer.

    • ceiling(x): Rounds up to the nearest integer.

    • trunc(x): Truncates to an integer by removing the decimal part.

  • Statistical Summary Functions

    • sum(x): Sum of elements.

    • prod(x): Product of elements.

    • mean(x): Mean of elements.

    • median(x): Median of elements.

    • min(x): Minimum value.

    • max(x): Maximum value.

    • range(x): Range (minimum and maximum).

  • Random Number Generator Functions

    • runif(n, min = 0, max = 1): Generates n random numbers from a uniform distribution between min and max.

    • rnorm(n, mean = 0, sd = 1): Generates n random numbers from a normal distribution with specified mean and sd (standard deviation).

    • rbinom(n, size, prob): Generates n random numbers from a binomial distribution with number of trials size and success probability prob.

    • sample(x, size, replace = FALSE): Randomly samples values from a vector x with specified size, with or without replacement (replace).

Some examples:

# Absolute of 2
abs(-2)
# Square root of 9
sqrt(9)
# Factorial of 4
factorial(4)
# Truncate of pi number
trunc(3.1416)
2
3
24
3
# Generation of 10 numbers sampled from a gaussian distribution with mean of 10 and standard deviation of 5
myvect<-rnorm(10, mean=10, sd=5)
myvect
# Mean 
mean(myvect)
# Median 
median(myvect)
  1. 17.3154600272022
  2. 2.33314450866007
  3. 2.65690099102852
  4. 17.8482261024962
  5. -0.877821425385072
  6. 16.0673664447932
  7. 11.2335652420367
  8. 13.3946342021372
  9. 1.33792095135807
  10. 10.1419139756298
9.1451311019957
10.6877396088333

Function help#

Important: If you ever want to inspect a function’s documentation, you can do this by typing ? followed by the function name (without parentheses).

Here are a couple of examples:

?lapply
?mean

Practice exercises#

Exercise 58

1- Create a function that takes a vector of numbers and returns the standardized values; that is, the data with the mean subtracted and rescaled so that the variance is 1.

# Your answers here

Exercise 59

2- Create a function “myabs” that returns the absolute value of a given number. Test that it works as expected.

# Your answers here

Exercise 60

3- Create a function named “mysquare”, which takes two arguments, such that it first takes the square of the first one, and then adds the second one to it. Set the second argument to take the value 2 as default. Test it.

# Your answers here