Exercise: If Statement

Use an if() statement to print a suitable message reporting whether there are any records from 2002 in the gapminder dataset. Now do the same for 2012.

Hint: use the any function.

library(gapminder) # install first if needed
data(gapminder)

Exercise: Loop and If Statements with gapminder

Write a script that loops through the gapminder data by continent and prints out whether the mean life expectancy is smaller or larger than 50 years.

Then, modify the script to loop over each country. This time print out whether the life expectancy is smaller than 50, between 50 and 70, or greater than 70.

Exercise: Loop with mtcars

Loop through the observations in the built-in mtcars and print the name of the car (it’s the rowname). For each car, also print out whether it has a “manual” or “automatic” transmission.

Hint: Look at the help page for mtcars to see which column refers to transmission type and which value equals which type.

data(mtcars) #load the data

Exercise: ifelse

Use the ifelse function to add a new column to mtcars, transmission_name, that contains “automatic” or “manual” as appropriate.

Exercise: Write Functions

Create a function that given a data frame will print the name of each column and the class of data it contains. An example test data frame is supplied below. Hint: Use mode() to get the class of the data in each column.

testdf <- data.frame(val1=1:5, val2=c("A","B","C","D","E"), 
                     stringsAsFactors = FALSE)

Create a function that given a vector and an integer will return how many times the integer appears in the vector.

Create a function that given a vector will print the mean and the standard deviation, it will optionally also print the median.

Challenge Exercise: The Birthday Problem

Say we choose 25 people at random. What is the probability two or more of them have the same birthday?

Write R code to figure this out by sampling birthdays (instead of solving it theoretically). Generalize your code to figure out how the probability changes as you increase the number of people in the room. Compute for 5 people up to 50 people, at increments of 5. If you know how to make a plot in R, plot the probability as a function of the number of people.

Model simplifications:

Hints:

Note: Learn more about the birthday problem here.

Challenge Exercise: Write a Substitution Cipher

Note: This challenge requires multiple functions/ideas not covered in the workshop.

Write a function that encodes a string with a substitution cipher.

Do this by:

Then also write a function that decodes from a supplied cipher text and an encoded string.

Check your functions by making them call each other.

Hint: You may want to convert all text to upper or lower case to keep everything consistent. There’s a toupper function. What are you going to do with spaces and punctuation?

Hint 2: Look at the sample function to randomly generate a cipher.

Hint 3: Use match to get the index position of a value in a vector.

Hint 4: strsplit – you can split on an empty string ''

Note: the answer provided doesn’t use it, but the chartr function might be useful.