# If you don't already have the tidyverse library installed,
# you will need to type install.packages("tidyverse") into the Console
library(tidyverse) 
## ── Attaching packages ──────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.3
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ─────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()

1. Changing the author field and file name.

(a) Change the author: field on the Rmd document from Your Name Here to your own name.
(b) Rename this file to “lab01_YourHameHere.Rmd”, where YourNameHere is changed to your own name.

2. Hello World!

Here’s an R code chunk that prints the text ‘Hello world!’.

print("Hello world!")
## [1] "Hello world!"

(a) Modify the code chunk below to print your name

print("Alexandra Chouldechova")
## [1] "Alexandra Chouldechova"


3. Creating sequences

We just learned about the c() operator, which forms a vector from its arguments. If we’re trying to build a vector containing a sequence of numbers, there are several useful functions at our disposal. These are the colon operator : and the sequence function seq().

: Colon operator:
1:10 # Numbers 1 to 10
##  [1]  1  2  3  4  5  6  7  8  9 10
127:132 # Numbers 127 to 132
## [1] 127 128 129 130 131 132
seq function: seq(from, to, by)
seq(1,10,1) # Numbers 1 to 10
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(1,10,2) # Odd numbers from 1 to 10
## [1] 1 3 5 7 9
seq(2,10,2) # Even numbers from 2 to 10
## [1]  2  4  6  8 10

To learn more about a function, type ?functionname into your console. E.g., ?seq pulls up a Help file with the R documentation for the seq function.

(a) Use : to output the sequence of numbers from 3 to 12

3:12
##  [1]  3  4  5  6  7  8  9 10 11 12

(b) Use seq() to output the sequence of numbers from 3 to 30 in increments of 3

seq(3, 30, 3)
##  [1]  3  6  9 12 15 18 21 24 27 30

(c) Save the sequence from (a) as a variable x, and the sequence from (b) as a variable y. Output their product x*y

x <- 3:12
y <- seq(3, 30, 3)
x * y
##  [1]   9  24  45  72 105 144 189 240 297 360


4. Cars data

We’ll look at data frame and plotting in much more detail in later classes. For a previous of what’s to come, here’s a very basic example.

For this example we’ll use a very simple dataset. The cars data comes with the default installation of R. To see the first few columns of the data, just type head(cars).

head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10

We’ll do a bad thing here and use the attach() command, which will allow us to access the speed and dist columns of cars as though they were vectors in our workspace.

attach(cars) # Using this command is poor style.  We will avoid it in the future.
speed
##  [1]  4  4  7  7  8  9 10 10 10 11 11 12 12 12 12 13 13 13 13 14 14 14 14
## [24] 15 15 15 16 16 17 17 17 18 18 18 18 19 19 19 20 20 20 20 20 22 23 24
## [47] 24 24 24 25
dist
##  [1]   2  10   4  22  16  10  18  26  34  17  28  14  20  24  28  26  34
## [18]  34  46  26  36  60  80  20  26  54  32  40  32  40  50  42  56  76
## [35]  84  36  46  68  32  48  52  56  64  66  54  70  92  93 120  85

(a) Calculate the average and standard deviation of speed and distance.

mean(speed)
## [1] 15.4
sd(speed)
## [1] 5.287644
mean(dist)
## [1] 42.98
sd(dist)
## [1] 25.76938



We can easily produce a histogram of stopping distance using the qplot function.

qplot(dist) # Histogram of stopping distance
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

The qplot(x,y,...) function can also be used to plot a vector y against a vector x. You can type ?qplot into the Console to learn more about the basic qplot function.

(b) Use the qplot(x,y) function to create a scatterplot of dist against speed.

qplot(speed, dist)