library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

R Markdown

This is an R Markdown file (it has a .Rmd file extension). When you execute code within the file, the results appear beneath the code.

R code goes in code chunks, denoted by three backticks. Try executing this chunk by clicking the Run button (a small green triangle) within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter (or Cmd+Shift+Enter on Mac).

ggplot(data = mpg) +
  geom_point(mapping = aes(x = cty, y = hwy), alpha = 0.2)

Add a new code chunk

Add a new code chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd/Ctrl+Option+I.

Put 2 + 2 in your new code chunk and run it.

PUT A NEW CHUNK HERE

Knitting R Markdown files

We’ll use R Markdown files as notebooks as we learn because we can record text, code and output.

R Markdown files are also a publication format. Try hitting the “Knit” button in the toolbar above. R runs all the code in the document from top to bottom, it collects the output and puts the code, text and output together in an HTML document—you should see it as 01-getting-started.html in the Files pane. This document is a great way to record or share your work (you can also Knit to PDF or Word documents).

Assigning variables

What’s the difference between the code in this chunk:

filter(mtcars, cyl == 4)
##                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Merc 240D      24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230       22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

And the code in this chunk?

four_cyls <- filter(mtcars, cyl == 4)

Functions

Look at the help page for seq

Add a chunk here that uses seq() to create a list of numbers from 5 to 100, spaced by 5 (so 5, 10, 15, 20, etc.)

CHUNK HERE

Syntax gone wrong

sd(pull(.data = starwars, var = mass)
## Error: <text>:2:0: unexpected end of input
## 1: sd(pull(.data = starwars, var = mass)
##    ^
my_name <- "Andrew'
## Error: <text>:1:12: unexpected INCOMPLETE_STRING
## 1: my_name <- "Andrew'
##                ^
pull(.data = "starwars", var = height)
## Error in UseMethod("pull"): no applicable method for 'pull' applied to an object of class "character"