Instructions

This homework assignment allows you to practice using R Markdown to create documents that combine text and R code. Your goal is to create an R Markdown file that produces a document that exactly replicates this one. Be sure to follow these steps:

The Assignment Operator

In the swirl tutorials, you have learned some basic R functionality. For example, you have learned how to assign values to an object using the <- operator:

x <- rnorm(100)

This creates a vector of 100 random values from a standard normal distribution. Note the spaces around the assignment operator. To make your code more readable, you should always use such spacing around operators. Typing the assignment operator is a bit of a pain, but in RStudio, there is a keyboard shortcut to produce <-. It is Alt -. This is very useful.

A Histogram

You can create a histogram of the standard normal values above with the code

Notice that I have displayed the code without actually evaluating it an showing the histrogram. Here is the histogram that would be produced:

hist(x)

Data Summaries

I can also talk about features of the data that I produced. For example, the mean is 0.0511863 and the standard deviation is 0.9468786. If I had a second variable

y <- rnorm(100)

I could even put those values in a table:

##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## -2.48623 -0.55124  0.11269  0.05058  0.77572  2.20365
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -1.8158 -0.4870  0.1622  0.1707  0.6951  2.8805


#### Packages

Let’s take a quick look at one of the datasets in the the ggplot2 package:

mpg
## # A tibble: 234 x 11
##    manufacturer model    displ  year   cyl trans   drv     cty   hwy fl    class
##    <chr>        <chr>    <dbl> <int> <int> <chr>   <chr> <int> <int> <chr> <chr>
##  1 audi         a4         1.8  1999     4 auto(l~ f        18    29 p     comp~
##  2 audi         a4         1.8  1999     4 manual~ f        21    29 p     comp~
##  3 audi         a4         2    2008     4 manual~ f        20    31 p     comp~
##  4 audi         a4         2    2008     4 auto(a~ f        21    30 p     comp~
##  5 audi         a4         2.8  1999     6 auto(l~ f        16    26 p     comp~
##  6 audi         a4         2.8  1999     6 manual~ f        18    26 p     comp~
##  7 audi         a4         3.1  2008     6 auto(a~ f        18    27 p     comp~
##  8 audi         a4 quat~   1.8  1999     4 manual~ 4        18    26 p     comp~
##  9 audi         a4 quat~   1.8  1999     4 auto(l~ 4        16    25 p     comp~
## 10 audi         a4 quat~   2    2008     4 manual~ 4        20    28 p     comp~
## # ... with 224 more rows

It contains observations collected by the US Environmental Protection Agency on 38 models of cars. Each car model has multiple variations, so there are many more than 38 rows. We can look at a histogram of the city gas mileage of each.

hist(mpg$cty, nclass = 12)