Pair Pogramming Exercises

In the following exercises, you will be working with your partner(s) through a series of exercises demonstrating how to write functions in R.
Do not worry if you do not get through all of the exercises. Switch the driver and navigator(s) roles after each exercise.

To make the coding window bigger, drag the little gray triangle in the bottom right corner of the code box

Exercise 1.1: Warmup (what are functions)

## Solved Example 1.1: Functions in R are 'code shortcuts' to common things you want to do.

Functions 'take' some inputs/variables in round brackets; then perform some operations on those inputs in { } and then return one value with return(...).

add <- function( first_argument, second_argument ) {
result <- first_argument + second_argument
return(result)
}

Once defined, you can use your function ('run it') many times with different inputs. This saves you copy-pasting code.

result1 <- add( 4, 6 )
result2 <- add( 9, 7 )
result3 <- add( result1, result2 )

Read the following code and discuss with your partner(s) what it does.

Exercise 1.2: Unit conversion

## Exercise 1.2: Create a function called `convert_temperature_CtoF` that takes a numeric value (a temperature in Celsius) as the argument, and returns the temperature in Fahrenheit

To get Fahrenheit you need to multiply Celsius by 9, then divide by 5 and then add 32.

e.g., 10 Celsius is 50 Fahrenheit because 10 * 9 / 5 + 32 = 50

Return values rounded with round( )

Exercise 1.3: Unit conversion

## Exercise 1.3: Now create a function for conversion the other way called `convert_temp_FtoC` that takes a numeric value (a temperature in Fahrenheit) as the argument.

To get this result, just flip the math from previous task. You can do it!

Return values rounded with round( )

Exercise 1.4: Unit conversion

## Exercise 1.4 Using your solutions above as a guide, create a function called `convert_length_INtoCM` that takes a numeric value (a unit of length in inches) as the argument.

# HINT: the inches to cm formula: cm = inches * 2.54

Exercise 2.1: Functions with Multiple Arguments

## Exercise 2.1 Create a function called `which_is_larger` which determines which of the 2 numeric inputs is larger.

Return a string telling what happened. See tests for the exact format of expected outputs.

Exercise 2.2: Functions with Multiple Arguments

## Exercise 2.2:

Create a function called `which_is_largest` that checks 3 numbers and tells you which one is largest by returning a character string 'first', 'second' or 'third'.

If there are ANY two identical numbers, just return a string "oh no!"

Exercise 3.1: Functions and Vectorisation

## Solved Exercise 3.1:

Complete two example functions `twice` and `is_positive` in the code box

Exercise 3.2: Functions and Vectorisation

## Exercise 3.2

One at the time, uncomment the tests ONE AT THE TIME and see what happens. Discuss with your partner(s).

~ Only after running the tests and exploring the output, read below ~


... oh no, an error! ...

Not to to fear!

Most functions in R are vectorized, meaning that the function will operate on all elements of a vector without needing to loop through and act on each element one at a time.

if else statements are *NOT* vectorized, hence the above error. However, the * (multiplication operator) is vectorized, which is why the twice function works with the values vector!

Vectorization is a real superpower of R! It makes writing code more concise, easy to read, and less error prone.

There is a vectorized if else statement in R, ifelse(), which we show in the is_positive_vectorized() function as well.

Exercise 4.1: Functions and Graphs

## Solved Example 4.1

In this exercise, we will be using a function to programmatically change the colors of a plot. Have a look at the starting code, discuss what you think it does with your partner(s), and move on to the next section.

Exercise 4.2: Functions and Graphs

## Exercise 4.2

It's your turn now!

Create a function called `categorize_and_color` that takes a number as input and returns a character string with the name of a color (e.g., 'red', 'orange' or 'green') based on some logic (decide on the logic with your partner(s)). It's up to you to come up with the logic rules.


All other code is already written for you - just complete the body of the function `categorize_and_color`.