-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_1_basics_of_R_code.qmd
254 lines (187 loc) · 4.87 KB
/
day_1_basics_of_R_code.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
---
title: "Basics of R code"
author: "Steen Flammild Harsted"
date: '`r Sys.Date()`'
output:
distill::distill_article:
self_contained: false
creative_commons: CC BY
format:
html:
code-fold: true
code-summary: "Show me the code"
toc: true
toc-depth: 2
number-sections: true
number-depth: 2
editor:
mode: source
execute:
output: false
---
```{r setup}
#| include: false
library(tidyverse)
```
\ \ \
# Functions
## `<-`
* Do you have a short cut for the assignment operator? (Tools -> Show Command Palette -> and type “assignment”)
* What does the assign operator `<-` do?
<br>
### Assign the inbuilt datset `iris` to an object called `my_data`
`iris` is available to you when you load R. It´s an inbuilt dataset. So it is available to you even though you cant find it in the Environment pane. Just type `iris`.
```{r}
my_data <- iris
```
<br>
### print `my_data`
```{r}
#| results: false
my_data
```
\ \
## `c()`
The c is short for [concatenate](https://www.merriam-webster.com/dictionary/concatenate), and means to link together.
This function combines values into a vector or list. For now you can think of a vector as a sequence of values.
The values are seperated by a `,`
<br>
### Use `c()` to create a sequence of numbers from 0 to 4
```{r}
c(0, 1, 2, 3, 4)
```
### Use `c()` to create a sequence of numbers from 0 to 4 in steps of 2
```{r}
c(0, 2, 4)
```
\ \
## `seq()`
Of course, such predictable operations can be done with ease in a more reliable way.
We will use the function `seq()` as an example. The output of this function is a sequence of numbers. The sequence of numbers is dictated by the arguments that you provide.
Read the arguments section on help page for `seq()`
Type `?seq()` in the console or type `seq()`and pres F1 while the cursor stands on the on the letters.
<br>
### Make a sequence of numbers that goes from 0 to 100
```{r}
#| results: false
seq(from = 0,
to = 100)
# This simple operation can also be done by just writing 0:100
```
<br>
### Make a sequence of numbers that goes from 0 to 100 in steps of 2
```{r}
#| results: false
seq(from = 0,
to = 100,
by = 2)
```
\ \
## `sample()`
### Using `sample()`, take a sample of 5 random numbers between 1 and 100
```{r}
sample(1:100, 5)
```
### What does the argument `replace` do? What is the default value?
\ \
## `mean()`
Another function we can use is `mean()`. This function gives you the mean value of a sequence of numbers.
Read the arguments section of the help page for `mean()`
<br>
### What type of input does the first arguments of the `mean()` function require?
```{r}
# It requires a vector.
# You can still think of this as a sequence of numbers
# c()
```
<br>
### Using `c()`, take the mean of the numbers 5, 3, 1, and 10
```{r}
mean(c(5, 3, 1, 10))
```
<br>
### Take the mean of this vector `c(2, 4, 6, NA)`
```{r}
mean(c(2, 4, 6, NA))
```
<br>
### Take the mean of this vector `c(2, 4, 6, NA)`, disregarding `NA` values
::: {.callout-tip collapse=true}
#### Hint
Read about the `na.rm` argument in the `mean()` function.
What is the default value?
:::
```{r}
mean(c(2, 4, 6, NA),
na.rm = TRUE)
```
<br>
### You will find the `na.rm` argument in many functions. It always defaults to FALSE. Discuss if this is a good idea? Is it different from other programs?
\ \
## `%>%`
* What is your keyboard shortcut to write the pipe `%>%` ?
(Pres CTRL+SHIFT+P and type **pipe** in the search field).
### Load the `tidyverse`
Why? The `%<%` is a part of the tidyverse, and is not included in Base R
```{r}
#| code-fold: false
library(tidyverse)
```
<br>
### **Using the pipe,** create a sequence of numbers going from 0 til 100 and then take the mean of the sequence.
```{r}
seq(from = 0,
to = 100) %>%
mean()
```
<br>
### **Using the pipe,** create a sequence of numbers going from 1 to 20 in 39 steps, and then sample 10 random numbers from the sequence
```{r}
seq(from = 1,
to = 20,
length.out = 39) %>%
sample(10)
```
<br>
### Rewrite this code to use the pipe instead
```{r}
#| code-fold: false
sort(
sample(seq(1:100),
10,
replace = TRUE)
)
```
```{r}
seq(1:100) %>%
sample(10,
replace = TRUE) %>%
sort()
```
\ \
## Explore
Investigate the following functions that we may need later on in this course.
* `quantile()`
* `rnorm()`
* `median()`
* `cumsum()`
* `min()`
* `max()`
* `n()`
* `set.seed()`
\ \ \
<script src="https://giscus.app/client.js"
data-repo="sorenoneill/r4phd"
data-repo-id="R_kgDOJ9etDQ"
data-category="Announcements"
data-category-id="DIC_kwDOJ9etDc4CYApF"
data-mapping="pathname"
data-strict="0"
data-reactions-enabled="0"
data-emit-metadata="0"
data-input-position="bottom"
data-theme="cobalt"
data-lang="en"
crossorigin="anonymous"
async>
</script>