Rainbow Deals - hours only!Up to 80% off on all courses and bundles.-Close
Creating functions
If statements
18. Summary


Great! Let's recap what we learned in this section:

Functions allow you to organize a set of instructions into reusable code.

  1. There are many built-in R functions that you can call, like substr(), length(), nrow(), data.frame(), c(), and others.
  2. In R, functions can take zero or more inputs (arguments) and return a single value.
  3. When creating a function, specify its name followed by the assignment operator (<-), opening and closing parentheses (with optional argument names in between), and the function body enclosed in braces.
  4. Function arguments in a function call can be provided by position:
    low_quality_column(survey$birthyear, 0.8)
    or by name:
    low_quality_column(threshold = 0.8, column = survey$birthyear)
  5. If-else statements can be used to control the flow of a program by executing code only when certain conditions are met.
  6. You can set default values of function arguments:
    low_quality_column <- function(column, threshold = 0.5){
      number_of_missings <- sum(is.na(column)) / length(column)
      return(number_of_missings > threshold)

Ready for some practice?


Click Next exercise.