Pre-Workshop Setup

Open up Socrative again

1a. Vectorization (5 minutes)

1. Conditional Statements (25 minutes)

---------- Socrative #1 ----------: Test whether number is between 45 and 50 (inclusive)

This is a tricky one, 3 correct answers

2. Loops (40 minutes)

---------- Socrative #2 ----------: For loop to calculate sum of vector

Nesting For loops

---------- Socrative #3 ----------: For loop to find years with life expectancy < 35

While Loops (5 minutes, optional)

3. Creating and using functions (30 minutes)

QUESTION: How many of you end up doing the same thing more than once while analyzing your data?

---------- Socrative #4 ----------: Testing variable scope

Both B and D are correct.

---------- Socrative #4 ----------: Write a C to F function

Function Challenge

Now write a function that takes two arguments: one the temp to be converted, and another that says whether to convert from fahrenheit to celsius or celsius to fahrenheit. Using if...then, make the same function do both.

Advanced Functions

4. Subsetting and reshaping data with dplyr and tidyr

dplyr

---------- Socrative #1 ----------: Filter using 2 filters and select 3 cols

Write a command with pipes that filters the gapminder dataset to only include data from 2007 in Africa, and then select the year, country, and lifeExp columns.

How many rows are left in the resulting dataset? If you're not sure how to find the number of rows, discuss with your neighbors.

---------- Socrative #2 ----------: Avg lifeExp by year for Africa

Let's compute the average life expectancy across all African countries by year. In how many years did average African life expectancy decrease?

tidyr

---------- Socrative #3 ----------: What format is the gap dataset

Answer: intermediate

---------- Socrative #4 ----------: Use dplyr on the long dataset

Using gap_long, summarize the mean life expectancy by continent

Resources:

5. Plotting and creating publication-quality graphics (60 minutes)

Question: How many of you have made a plot in R? How many of you have used ggplot?

---------- Socrative #1 ----------: Modify a ggplot graph

---------- Socrative #2 ----------: Color by continent & add separate trends

---------- Socrative #3 ----------: Optional ggsave & ggplot variables - ggsave will overwrite the graph

Facets (if time permits)

Alternate: Facets with dplyr (if time permits)

GGplot2 Challenge #2: write a function that takes vector of countries & creates faceted graph

GGplot2 Challenge #2:

GGplot2 Challenge #3:

Resources

6. Producing reports and notebooks with knitr (20 minutes)

Question: How many of you could just send your code to your advisor or collaborator?

---------- Socrative ----------: R Notebook Challenge

Modify your R notebook to load the data from gap, show the first 10 lines, and display a graph.
Give each R code section a heading and short summary. -

    ```
    ## Reading & Displaying Data

    This chunk reads in the gap dataset and shows the first 10 lines

    ``{r}
    gap <- read.csv("data/gapminder-data.csv")
    head(gap, 10)
    ``

    ## Graph Gap Dataset

    This chunk shows a graph

    ``{r}
    library(ggplot2)
    ggplot(data = gap, aes(x = gdpPercap, y = lifeExp)) + 
      geom_point()
    ``
    ```

7. Writing good software (5 Minutes)

8. Using Git from RStudio (15 minutes)