Question 2, part c

The first key to this question was to realize that the functions gather and spread are called with a parantheses.

Okay to omit -e in the first solution.
grep -n -e 'gather(\|spread(' *.R* or grep -n -E 'gather\(|spread\(' *.R*

On the in-class midterm, we were not looking for -n though it is good to include it.

Here are nother solutions from your peers, each using a pipe:

*grep -n '^[^#]' *.R* | grep -e 'gather(\|spread('*

or, similarly,

grep -e 'gather(\|spread('* *.{R, Rmd} | grep -v "^[^#]".

Question 4, part d

These answers assumes this was independent of part “b”.

Option 1.

Orange %>%
 group_by(Tree) %>%
 filter( age == max(age) ) %>% 
 ungroup() %>%
 summarize( avg_growth_rate = mean( circumference / age ) ) #mutate okay too

Option 2.

Orange %>%
 group_by(Tree) %>%
 summarize( growth_rate = max(circumference) / max(age) ) %>%
 summarize( avg_growth_rate = mean(growth_rate) )

Option 3.

Orange %>%
 arrange(Tree, age) %>% 
 group_by(Tree) %>%
 summarize( growth_rate = circumference[n()] / age[n()] ) %>%
 summarize( avg_growth_rate = mean(growth_rate) )

Question 5

Please refer to q5_solutions.R at the course repo for the solution.

Question 6

Task 1

runs = sim_nine(n * 9, team = team)
dim(runs) = c(9, n)
colSums(runs)

Task 2

runs_mw = mean(games_mw)
runs_dd = mean(games_dd)
runs_tt = mean(games_tt)

Task 3

mw_wins = 81 * mean(mw_vs_dd == 'mw') + 81 * mean(mw_vs_tt == 'mw')
dd_wins = 81 * mean(mw_vs_dd == 'dd') + 81 * mean(dd_vs_tt == 'dd')
tt_wins = 81 * mean(mw_vs_tt == 'tt') + 81 * mean(dd_vs_tt == 'tt')