-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntroToR_SOLVED.Rmd
454 lines (308 loc) · 6.93 KB
/
IntroToR_SOLVED.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
---
title: "IntroductionToR"
author: "Maja Kuzman"
date: "7/1/2020"
output: html_document
---
# R, Rstudio, Rmd
---
### How can we use R and RStudio?
- console
- script
- notebook:
new chunk: ctrl + ALT + i
--
### What can we do with R?
- We can use R as a calculator!
### Try it:
Add a new chunk (ctrl + ALT + i).
Calculate the result of 13%/%3 and 13%%3.
( 13/3 = 4*3+1 )
```{r}
13%/%3
13%%3
```
*We can even do better than that...*
# Variables and data structures
### Variables
```{r}
myFirstNumber <- 0.1
myFirstVector <- c(2, 3, 7, 8)
```
-> call the variable by name
-> print() function
-> one line, part of code shaded and CTRL +ENTER
```{r}
print(myFirstNumber)
myFirstNumber
```
### Data structures in R
vectors
matrix
data.frame
list
factors
# Vectors
## The basics
### What we can do with vectors:
#### Make a vector c()
You can make a vector by using the function c() (concatenate). Here is an example of vectors myFirstvector, and myFirstSequence:
```{r}
myFirstVector <- c("some words","p","word", "last one")
myFirstSequence <- 1:4
```
---
## Subsetting [ ]
Print the whole vector
```{r}
myFirstVector
myFirstSequence
```
Print third element in a vector
```{r}
myFirstVector[3]
myFirstSequence[3]
```
---
### Access multiple elements:
1) BY POSITION
Provide a vector of positions to look at:
```{r}
myFirstVector
```
```{r, eval=FALSE}
myFirstVector[1,3]
```
```{r}
myFirstVector[c(1,3)]
```
```{r}
somePositions <- c(1,3)
somePositions
```
```{r}
myFirstVector[somePositions]
```
---
### Access multiple elements:
2) BY INCLUSION
Provide a LOGICAL vector :
```{r}
myFirstVector
myFirstVector[c(TRUE, FALSE, TRUE,FALSE)]
```
```{r}
someLogicalVector <- c(TRUE,TRUE, FALSE, FALSE)
someLogicalVector
```
```{r}
myFirstVector[someLogicalVector]
```
---
### Exercise!
1. Create a vector named myvector that contains numbers 15,16,17,18 and 20.
2. Get 2nd and 4th number in the vector by subsetting.
3. Get 2nd and 4th number in the vector by providing a logical vector.
Solution:
CTRL+Alt+i will insert a new chunk
```{r}
myvector <- c(15,16,17,18,20)
myvector <- c(15:18,20)
myvector[c(2,4)]
myvector[c(F,TRUE,FALSE,T)]
```
---
### Think about it!
#### What happened here??:
```{r, eval=FALSE}
someothervector <- c(1,0,1,0,1)
myFirstVector
myFirstVector[someothervector]
myFirstVector[as.logical(someothervector)]
```
--
### Basic operation on vectors
Same as on numbers:
+
-
/
*
Example:
#### Multiplication by constant
```{r}
someothervector
someothervector * 0.5
```
---
#### Multiplication by other vector:
I) SAME size
```{r}
someothervector
1:5
someothervector * 1:5
```
II) DIFFERENT size : recycling *because why not.*
```{r}
someothervector
c(0.3,0.1)
someothervector*c(0.3,0.1)
# c(1,0,1,0,1)* c(0.3, 0.1, 0.3, 0.1,0.3, 0.1)
c(1,2,3,4,5,6) * 1:2 # doesnt produce a warning c(1,2,3,4,5,6) * c(1,2, 1,2, 1,2)
```
---
### Basic comparisons
The following will return a logical vector for every compared position:
```{r}
someothervector
someothervector == 1
```
```{r}
someothervector == c(1,0,1,0,1)
```
```{r}
someothervector
someothervector>0
```
--
#### what happened here?
```{r}
someothervector[someothervector>0]
```
*remember: subsetting by logical indices returns ELEMENTS on POSITIONS WHICH ARE TRUE*
---
### Exercise: Multiplication, recycling and comparison.
1. Multiply your myvector by c(0.1, 0.2)
-> what do you expect to get??
2. Check if you get what you expected by comparing it to vector you expect to get :)
Solution:
(get new chunk by ctrl+alt+i)
```{r}
result <- myvector * c(0.1,0.2)
expecttoget <- c(1.5,3.2,1.7,3.6,2.0)
result
expecttoget
result == expecttoget
```
---
### Exercise: Multiplication, recycling and comparison.
1. Return only ELEMENTS in myvector that are smaller then 17.
-> hint - use subsetting and comparison.
```{r, eval=FALSE}
myvector[ myvector<17 ]
```
---
### Logical operators
AND: &
```{r, echo=FALSE}
data.frame('first_second'=c(TRUE, FALSE),
'TRUE'=c(TRUE, FALSE),
'FALSE'=c(FALSE, FALSE))
```
OR: |
```{r, echo=FALSE}
data.frame('first_second'=c(TRUE, FALSE),
'TRUE'=c(TRUE, TRUE),
'FALSE'=c(TRUE, FALSE))
```
NOT: !
TRUE -> FALSE
FALSE -> TRUE
They are used in a following way:
```{r, eval=FALSE}
firstLogical <- c(TRUE, TRUE, FALSE, FALSE)
secondLogical <- c(TRUE, FALSE, TRUE, FALSE)
firstLogical & secondLogical
firstLogical | secondLogical
! firstLogical
```
---
### Exercise: Subset by logical indexes
remember:
x == 0 - where is x equal to 0
x%%5 -> gives you the modulo while dividing x by 5
x != 0 - not equal to
1. Return all the elements from your myvector that are divisable by 3.
```{r}
myvector
myvector%%3 == 0
myvector[myvector%%3 == 0]
```
extra:
2. Return all the elements from your myvector which are on EVEN positions (2,4,6,...)
```{r}
myvector[c(2,4)]
myvector[c(F,T,F,T,F)]
myvector[c(F,T)] # because why not (recycling)
```
## Functions
#### Some useful functions
---
### length - Gives you the length of the vector:
```{r}
length(someothervector)
```
--
### unique -Gives you all unique elements in your vector:
```{r}
unique(someothervector)
```
--
### table : gives you list of all elements and counts them
```{r}
table(someothervector)
```
---
## Math:
```{r}
sum(someothervector)
mean(someothervector)
sd(someothervector)
summary(someothervector)
```
---
## more random math:
### sample - gives you random numbers from a vector
```{r}
sample(1:100, 10)
```
--
### rnorm - gives you 10 random numbers from normal distribution with mean=0 and sd=1
```{r}
rnorm(10, mean = 0, sd = 1)
```
--
analogously, rpois, runif
```{r,eval=FALSE}
runif(n = 10, min=10, max=20)
```
---
## Exercise!
What will the following produce?
x=seq(1,6, by=2)
x[x>4]
mean(x)
median(x)
x[c(T,F)]
rev(x)
Create a sequence of numbers from 1 to 1000 and save it in a vector myseq.
```{r}
myseq <- 1:1000
```
Part 1: What is the mean of the elements which are larger then the median?
```{r}
mean(myseq[myseq>median(myseq)])
```
Part 2: What is the sum of every second element?
```{r}
sum(myseq[c(T,F)])
```
Part 3: sum all the elements pairwise by summing the first one with the last one,
second one with second to last, and so on.
```{r}
myseq+rev(myseq)
```
### HELP!!??!!
```{r, eval=FALSE}
?runif
example(runif)
```