Aan de slagGa gratis aan de slag

Write your own function

Wow, things are getting serious… you're about to write your own function! Before you have a go at it, have a look at the following function template:

my_fun <- function(arg1, arg2) {
  body
}

Notice that this recipe uses the assignment operator (<-) just as if you were assigning a vector to a variable for example. This is not a coincidence. Creating a function in R basically is the assignment of a function object to a variable! In the recipe above, you're creating a new R variable my_fun, that becomes available in the workspace as soon as you execute the definition. From then on, you can use the my_fun as a function.

Deze oefening maakt deel uit van de cursus

Intermediate R

Cursus bekijken

Oefeninstructies

  • Create a function pow_two(): it takes one argument and returns that number squared (that number times itself).
  • Call this newly defined function with 12 as input.
  • Next, create a function sum_abs(), that takes two arguments and returns the sum of the absolute values of both arguments.
  • Finally, call the function sum_abs() with arguments -2 and 3 afterwards.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Create a function pow_two()



# Use the function


# Create a function sum_abs()



# Use the function
Code bewerken en uitvoeren