-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01b_r-basics.qmd
255 lines (168 loc) · 7.22 KB
/
01b_r-basics.qmd
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# R Basics
## Using R in the console
> The most basic way to interact with R is to type code directly in the console
>
> - type expression to evaluate
> - hit return
> - output of the evaluation of the expression is printed to the console below
The simplest thing you could do with R is do arithmetic.
```{r}
1 + 100
```
If you type in an incomplete command, R will wait for you to complete it:
```{r, eval=FALSE}
1 +
```
```
+
```
Any time you hit return and the R session shows a `+` instead of a `>`, it means it's waiting for you to complete the command. If you want to cancel a command you can hit `Esc` and RStudio will give you back the `>` prompt.
## Working in scripts
> To make code and workflow reproducible and easy to re-run, it's better to **save code in a script** and use the script editor to edit it. This way, there is a complete record of the analysis.
### Creating a new script
Click on **File \> New File \> R Script**. Click on the save icon or (like any other file) using keyboard shortcut `CTRL / CMD + S`
![](assets/script_new_create.png) ![](assets/script_new_save.png)
![](assets/script_new_open.png)
### Executing commands from scripts
RStudio allows you to execute commands directly from the script editor by using `Ctrl` + `Enter` shortcut (on Macs, `Cmd` + `Return` will work, too).
When you execute command from a script, the line of code in the script indicated by the cursor or all of the commands in the currently highlighted will be sent to the console. You can find other keyboard shortcuts in **Tools \> Keyboard Shortcuts Help** or in the [RStudio IDE cheatsheet](https://github.com/rstudio/cheatsheets/raw/master/rstudio-ide.pdf).
![](assets/script_exe.png)
### Comments
You can add comments to your code by using a hash symbol `#`. Any text on a line of code following `#` is ignored by R when it executes code.
```{r}
1 + 10 # this text does nothing
```
## Using R as a calculator
When using R as a calculator, the order of operations is the same as you would have learned back in school.
From highest to lowest precedence:
- Parentheses: `(`, `)`
- Exponents: `^` or `**`
- Multiply: `*`
- Divide: `/`
- Add: `+`
- Subtract: `-`
```{r}
3 + 5 * 2
```
Use parentheses to group operations in order to force the order of evaluation if it differs from the default, or to make clear what you intend.
```{r}
(3 + 5) * 2
```
Really small or large numbers get a scientific notation:
```{r}
2/10000
```
Which is shorthand for "multiplied by `10^XX`". So `2e-4` is shorthand for `2 * 10^(-4)`.
You can write numbers in scientific notation too:
```{r}
5e3 # Note the lack of minus here
```
## Mathematical functions
R has many built in mathematical functions. To call a function, we can type its name, followed by open and closing parentheses. Anything we type inside the parentheses is called the function's arguments:
```{r}
sin(1) # trigonometry functions
```
```{r}
log(1) # natural logarithm
```
```{r}
log10(10) # base-10 logarithm
```
```{r}
exp(0.5) # e^(1/2)
```
::: callout-tip
**Don't worry about trying to remember every function in R**. You can look them up on Google, or if you can remember the start of the function's name, use the tab completion in RStudio.
This is **one advantage that RStudio has over R** on its own, it has **auto-completion abilities** that allow you to more easily look up functions, their arguments, and the values that they take.
**Typing a `?` before the name of a command will open the help page** for that command. As well as providing a detailed description of the command and how it works, scrolling to the bottom of the help page will usually show a collection of code examples which illustrate command usage. We'll go through an example later.
:::
## Variables and assignment
We can store values in variables using the assignment operator `<-`, like this:
```{r}
x <- 1/40
```
Notice that **assignment does not print a value**. Instead, we stored it for later in something called a **variable**. `x` now contains the **value** `0.025`:
```{r}
x
```
Look for the `Environment` tab in one of the panes of RStudio, and you will see that `x` and its value have appeared. Our **variable `x` can be used in place of a number in any calculation that expects a number**:
```{r}
log(x)
```
Notice also that **variables can be reassigned:**
```{r}
x <- 100
x
```
`x` used to contain the value 0.025 and now it has the value 100.
Assignment values can contain the variable being assigned to:
```{r}
x <- x + 1 #notice how RStudio updates its description of x on the top right tab
y <- x * 2
```
- The right hand side of the assignment can be any valid R expression.
- The right hand side is *fully evaluated* before the assignment occurs.
### On variable names
Variable names can contain **letters, numbers, underscores and periods**. They cannot start with a number nor contain spaces at all.
Different people use different conventions for long variable names, these include
- periods.between.words
- camelCaseToSeparateWords
- **snake_case: underscores_between_words**
While I suggest you use snake_case, ultimately what you use is up to you, but **be consistent**.
Even more important is that **variable names are descriptive enough** of what they contain. So long and descriptive is better than shorter and cryptic.
## Comparing things
We can also do comparison in R:
```{r}
x <- 1
```
```{r}
x < 2 # less than
```
```{r}
x <= 1 # less than or equal to
```
```{r}
x > 0 # greater than
```
```{r}
x >= -9 # greater than or equal to
```
```{r}
x <- "apple"
x == "apple" # equality (note two equals signs, read as "is equal to")
x == "orange"
```
```{r}
1 != "orange" # inequality (read as "is not equal to")
```
```{r}
x %in% c("apple", "orange") # membership (read as "is member of")
```
::: callout-caution
A word of warning about comparing numbers: you should **avoid using `==` to compare two numbers unless they are integers** (a data type which can specifically represent only whole numbers).
This is because of the way that R stores floating point numbers (i.e. of data type `"double"`).
To compare floating point numbers **safely**, you should use the `all.equal` function, which is designed to compare floating point numbers within a certain tolerance. \_
```{r}
x <- 1.02
all.equal(x, 1.02)
```
:::
### Combining comparisons with `&` and `|`
We can combine comparisons using the `&` and `|` operators:
#### `&` (AND)
`&` is effectively translated as **"and"**. When using `&` ALL conditions must be met for the whole statement to evaluate to `TRUE`.
```{r}
x <- 4
x > 5 & x < 10
```
In this case, because we are using the `&` operator, the result in `FALSE` because, although `x` is less than 10, it is not greater than 5. Both conditions must be met for the whole statement to evaluate to `TRUE`.
#### `|` (OR)
`|` is translated as "or" and only a single condition needs to be met for the whole statement to evaluate to `TRUE`.
```{r}
x > 5 | x < 10
```
This time, because we are using the `|` operator, the condition is met if either `x > 5` or `x < 10` is true. In our case, although `x` is not greater than 5, it is less than 10, so the statement to evaluates to `TRUE`.
## Base R Cheatsheet
```{r, echo=FALSE}
knitr::include_url("assets/cheatsheets/r-cheat-sheet-3.pdf")
```