-
Notifications
You must be signed in to change notification settings - Fork 16
/
01-Getting-started.Rmd
76 lines (52 loc) · 2.1 KB
/
01-Getting-started.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
---
title: "Getting Started with R and RStudio"
output: html_document
---
<!-- This file by Charlotte Wickham is licensed under a Creative Commons Attribution 4.0 International License. -->
```{r setup}
library(tidyverse)
```
## R Markdown
This is an [R Markdown](http://rmarkdown.rstudio.com) 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).
```{r}
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.
```{r}
2 + 2
```
## Knitting RMarkdown files
We'll use RMarkdown files as notebooks as we learn because we can record text, code and output.
RMarkdown 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:
```{r}
filter(mtcars, cyl == 4)
```
And the code in this chunk?
```{r}
four_cyls <- filter(mtcars, cyl == 4)
```
## Function syntax
Take a look at this code:
```{r}
heights <- pull(.data = starwars, var = height)
mean(heights, na.rm = TRUE)
```
1. What functions are being called?
2. What arguments do they take?
3. What values are being passed as which arguments?
## Syntax gone wrong
```{r, error=TRUE}
sd(pull(.data = starwars, var = mass)
```
```{r, error=TRUE}
my_name <- "Charlotte'
```
```{r, error=TRUE}
pull(.data = "starwars", var = height)
```