11.5 Exercises

ds4psy: Exercises on Functions (11)

The following exercises practice how functions can be defined and evaluated and how the flow of information can be controlled.

11.5.1 Exercise 1

Fun with errors

Imagine someone proudly presents the following 3 functions to you. Each of them takes a vector v as an input and tries to perform a simple task. For each function:

  • describe the task that the function is designed to perform,
  • test whether it successfully performs this task,
  • name any problem that you detect with the current function,
  • fix the function so that it successfully performs its task.

11.5.2 Exercise 2

Conditional feeding

Let’s write a first function and then add some conditions to it.

  1. Write a function feed_me that takes a character string food as a required argument, and returns the sentence "I love to eat ___!". Test your function by running feed_me("apples"), etc.

Here’s a template with some blanks, to get you started:

  1. Modify feed_me so that it returns "Nothing to eat." when food = NA.

  2. Extend your function to a feed_vegan function that uses 2 additional arguments:

  • type should be an optional character string, set to a default argument of "food". If type is not "food", the function should return "___ is not edible.".

  • vegan should be an optional Boolean value, which is set to FALSE by default. If vegan is TRUE, the function should return "I love to eat ___!". Otherwise, the function should return "I do not eat ___.".

Test each of your functions by evaluating appropriate function calls.

11.5.3 Exercise 3

Buggy number recognition

This exercise analyzes and corrects someone else’s function.

  1. Explain what the following function describe (not to be confused with describe above) intends to do and why it fails in doing it.
  1. Repair the describe function to yield the following results:
  1. What are the results of describe(NA) and describe("one")? Correct the function to yield appropriate results in both cases.

  2. For what kind of x will describe print "x is beyond description."?

11.5.4 Exercise 4

Randomizers revisited

In Chapter 1, we explored the ds4psy functions coin(), dice(), and dice_2(), and used the base R function sample() to mimic their behavior (see Section 1.4.6 and Exercise 3 in Section 1.8.3). Now we can create these functions.

  1. Study the dice() function of ds4psy and write a function my_dice() that accepts one argument N and always returns N random numbers from 1 to 6 (as a number).

Hint: Drawing random numbers from a uniform distribution could be achieved by stats::runif(), but beware of distortions when rounding its results. An easier solution uses the base::sample() function in its definition.

  1. Use (parts of) your new my_dice() function to write a my_coin() function that mimicks the behavior of the ds4psy coin() function.

Hint: The sampling part of the function remains the same — only the events to sample from change.

  1. Bonus task: We have seen (in Exercise 3, Section 1.8.3) that the dice_2() function of ds4psy yields non-random results. Write a similar my_special_dice() function that accepts 2 arguments:
  • N is the number of dice to throw. Each dice should always yield a number from 1 to 6 and your function should return the N numbers of the set of dice.

  • The 2nd argument special_number lets you specify a number (from 1 to 6) that will occur twice as often as the other numbers in exactly one of the dice.

  • To make this scam less obvious, your function should return the results of the N dice in a random order.

Hint: We can rely on the dice() function to imitate all dice. For the one “special” dice, we need to change the events to sample from so that the special_number occurs twice as often as the other numbers. Randomizing the order of outputs can be achieved by the sample() function (by drawing N dice from the set of all dice without replacement).

  1. Bonus task:
  • How could you check whether your my_special_dice() function works as intended?

Hint: Don’t solve it here — just describe what you would need to check your function.

11.5.5 Exercise 5

Tibble charts

This exercise writes a function to extract rows from tabular inputs based on the top values of some variable.

  1. Write a top_3 function that takes a tibble data and a the column number col_nr of a numeric variable as its 2 inputs and returns the top-3 rows of the tibble after it has been sorted (in descending order) by the specified column number.

Use the data of sw <- dplyr::starwars to illustrate your function.

Hint: To write this function, first solve its task for a specific case (e.g., for col_nr = 2). When using the dplyr commands of the tidyverse, a problem you will encounter is that a tibble’s variables are typically referenced by their unquoted names, rather than by their number (or column index). Here are 2 ways to solve this problem:

  • To obtain the unquoted name some_name of a given character string "some_name", you can call !!sym("some_name").

  • Rather than aiming for a tidyverse solution, you could solve the problem with base R commands. In this case, look up and use the command order to re-arrange the rows of a tibble or data frame.

  1. What happens in your top_3 function when col_nr refers to a character variable (e.g., dplyr::starwars[ , 1])? Adjust the function so that its result varies by the type of the variable designated by the col_nr argument:
  • if the corresponding variable is a character variable, sort the data in ascending order (alphabetically);
  • if the corresponding variable is a numeric variable, sort the data in descending order (from high to low).
  1. Generalise your top_3 function to a top_n function that returns the top n rows when sorted by col_nr. What would be a good default value for n? What should happen when n = NA and when n > nrow(data)?

Check all your functions with appropriate inputs.

Note: Functions for different tasks and data types

The following three exercises illustrate how functions can use, mix, and merge various data types to solve different tasks. Specifically, they ask you to write functions for

  • visualizing data as plots (Exercise 6),
  • printing numbers as text (Exercise 7),
  • computing with dates (Exercise 8).

11.5.6 Exercise 6

A plotting function

This exercise asks you to write a function that uses some input data for creating a specific type of plot.

  1. Write a function plot_scatter that takes a table (tibble or data frame) with 2 numeric variables x and y as my_data and plots a scatterplot of the values of y by the values of x.

Hint: First write a ggplot command that creates a scatterplot of my_data. Then wrap a function plot_scatter around this command that takes my_data as its argument.

Test your function by using the following 2 tibbles tb_1 and tb_2 as my_data:

  1. For any table my_data that contains 2 numeric variables x and y we can fit a linear model as follows:

Incorporate the fit of a linear model into your plot_scatter function. Use a linear model to add a line to your plot that shows the prediction of the linear model (in a color that can be set by an optional col argument).

11.5.7 Exercise 7

Printing numbers (as characters)

A common problem when printing numbers in text is that the number of digits to be printed (i.e., characters or symbols) depends on the number’s value. This means that series of different numbers often have different lengths, which makes it hard to align them (e.g., in tables). A potential solution to this is adding leading or trailing zeros (or empty spaces) to the front and back of a number.

The function num_as_char() of the ds4psy package provides a (sub-optimal) solution to this problem by containing 3 main arguments:

  • x for the number(s) to be formatted (required);
  • n_pre_dec for the number of digits prior to the decimal separator (default n_pre_dec = 2);
  • n_dec to specify the number of digits after the decimal separator (default n_dec = 2).

Additional arguments specify the symbol sym to use for filling up digit positions and the symbol used as decimal separator sep.

  1. Experiment with num_as_char() to check its functionality and limits.
  1. Write your own function num_to_char() that achieves the same (or a similar) functionality.

Hint: The num_as_char() function of the ds4psy package also works for vectors, but uses 2 for loops to achieve this. Try writing a simpler solution that works for using individual numbers as x (i.e., scalars, or vectors of length 1). If you get stuck, try adapting parts of the solution used by num_as_char.

11.5.8 Exercise 8

Computing with dates

  1. Use what you have learned in Chapter 10: Time data to write a function that takes a date or time (e.g., the date of someone’s birthday) as its input and returns the corresponding age (as a number, rounded to completed years) as output.

  2. Check your function with appropriate examples.

  1. Does your solution also work when multiple dates or times are entered (as a vector)?

  2. Bonus task: Write alternative versions of the following lubridate functions:

  • a my_leap_year() function (as an alternative to lubridate::leap_year()) that detects whether a given year is a leap year (see Wikipedia: leap year for definition).

  • a my_change_tz() function (as an alternative to lubridate::with_tz()) that converts the time display from its current time zone into a different one (tz), but keeping the point in time (i.e., the represented time) the same.

  • a my_change_time() function (as an alternative to lubridate::force_tz()) that changes a given time into the same nominal time (i.e., showing the same time display, but representing a different time) in a different time zone (tz).

Hints: See Section 10.3.4 of Chapter 10 for examples and the corresponding lubridate functions. Check your functions for a variety of examples and input types.

This concludes our exercises on creating new functions.