The following are a set of self-test questions designed to complement the advancedConcepts tutorial. These exercises are only meant to be attempted after you have finished the advancedConcepts tutorial.
The only way to learn a new programming language is by writing programs in it. The first program (function) to write is the same for all languages. You must print the words, "Hello, world." This is a sacred rite of passage, cherish it!
- Write a function that returns the phrase "Hello, World."
-
Load the
iris
dataset we used in the earlier tests. Write a function that takesiris
as its argument, and returns three subsets of the data.frame split by the three different types of species (saved as a single object). -
Write a function that takes
iris
as its argument. The function should, for each row, add Sepal.Length and Petal.Length if Sepal.Width is > 3.1. It should substract Petal.Length from Sepal.Length if Sepal.Width is <3.1. The answer should be returned as a vector. -
Load the
mtcars
dataset we used in the earlier tests. Write a function that takes a number of cylinders as its argument. Have the function return the average miles per gallon (column mpg) for all cars with that many cylinder (column cyl). -
Write a function that simulates 1,000,000 powerball drawings. A powerball drawing takes a random sample of 5 numbers (without replacement) from 1 through 69, plus one powerball number ranging from 1 through 26. The function should return a single object recording all of your draws.
-
Write a fucntion that take a single set of lottery numbers (as a vector) as its argument. As before, write a function that simulates 1,000,000 powerball drawings. Have the function return a
TRUE
orFALSE
value if you won any of the drawings.