Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Creating functions
5. Function creation
If statements
Summary

Instruction

Great job! Take a look at another example of a function:

distinct_column_values <-function(column){
  distinct_values <- length(table(column))
  return(distinct_values)
}

This function counts the number of different values in the given column. It takes one input: the column for which we want to count the number of different values.

The function has two instructions. The first instruction creates a variable named distinct_values by counting the number of different values stored in the given column. This is accomplished via the table() and length() functions. The second instruction is a return statement that denotes which value should be returned as the function's output (in our case, the distinct_values variable).

Exercise

Let's practice function creation. Create a function named count_age() that takes one input named birth_year and returns that person's current age.

For this calculation, you can use the current_year() function that you see in the code editor. The count_age() function should return the difference between current_year() and birth_year.