Question 1

Imagine someone flips a coin 100 times.

You gain a dollar every time the coin comes up heads, and you lose a dollar every time it comes up tails.

You start off with $50, and you want to keep track of your money over time.

So you decide to create a plot showing how much money you have over time.

Can you use R to simulate this situation? (the plot and all)

Hint:

This question can be done succinctly using the following commands, but complete it however you see fit:

runif() or sample(),
ifelse(),
cumsum(),
plot()

Your plot should look something like like this (although the data will be different every time)

Bonus:

Make this graph twice, once using plot() and once using ggplot.

Question 2

Research the following dplyr “verbs” and give brief explanations for each.

Bonus:

inner_join and outer_join

First describe what “join” does and then describe the difference between inner joins & outer joins.


Question 3

Download the mcd.tsv file from the website and put it into the directory you’ve been using throughout this seminar.
Run the following command to load the data into R.

mcd <- read.delim("mcd.tsv",header=TRUE,sep = "\t")
  1. Using this file (it’s a dataset that shows McDonald’s nutrition content), use dplyr to show the average calories (CAL) by food category (CATEGORY).

  2. Add columns for the minimum and maximum calories by category to this table.

  3. Run the following lines of code. Describe what each of them show.
    1. qplot(CAL,data=mcd)
    2. qplot(CAL,SGR,data=mcd)
    3. qplot(CAL,SGR,color=CATEGORY,data=mcd)

  4. Make your own interesting plot using the McDonald’s dataset and ggplot.

Question 4

Download the nba.csv file from the website and load it into R.

a) Using the NBA dataset provided, use dplyr to find the player with the highest total fouls.

b) Find the average amount of points scored each season by player.

c) Create a table that shows the top scoring player for each season.

d) What player(s) was the top scorer for the most seasons?



Question 5

  1. Using the nycflights13 data package, use the table flights to find the average distance flown by carrier

  2. What data had the highest mean departure delay (dep_delay)?

Bonus that you should really try to attempt.


Use both the flights and planes data table to answer this. Create a table with two columns: mean distance flown and number of seats. This table will be grouped by seat.

Hint: you will have to use a join command to join the two tables.