Chapter 5 Advanced Use

1. Define a function myDiff that computes the difference between two numbers. (Find help in Chapter 5.1.1)

 

2. Define a function nonNegativeDiff that takes two numbers and gives their non-negative difference, i.e. always subtracts the lesser from the greater of the two numbers. (Find help in Chapter 5.1.2)

 

3. Define a function myFactorial that computes the factorial (n!) of a positive integer using a loop. (Find help in Chapter 5.1.3)

 

4. Create a loop that prints all squares of the natural numbers where the square is smaller than 1000. How many squares satisfy this condition? Answer this question with your loop. (Find help in Chapter 5.1.3)

 

5. Load the package ggplot2 (install beforehand if necessary) and make yourself familiar with the data set msleep using ?msleep. (Find help in Chapter 5.2)

 

6. Create a boxplot with sleep_total on the y-axis and vore on the x-axis using ggplot2. Start by creating an empty plot with msleep supplied to the data= argument and then add a geom_boxplot(). (Find help in Chapter 5.2)

 

7. Modify the previously generated plot by adding a layer of points over the boxplot, using geom_jitter and the same aesthetics as for geom_boxplot. (Find help in Chapter 5.2)

 

8. Now modify the aesthetics of geom_boxplot and geom_jitter in such a way that they are colored according to the variable vore. (Find help in Chapter 5.2)

 

9. Finally, add suitable axis labels to the plot. (Find help in Chapter 5.2)