Great! Let's recap what we learned in this section:
Functions allow you to organize a set of instructions into reusable code.
- There are many built-in R functions that you can call, like
substr()
, length()
, nrow()
, data.frame()
, c()
, and others.
- In R, functions can take zero or more inputs (arguments) and return a single value.
- 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.
- 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)
- If-else statements can be used to control the flow of a program by executing code only when certain conditions are met.
- 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?