/
code_during_talk.R
88 lines (61 loc) · 1.25 KB
/
code_during_talk.R
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
# Libraries ---------------------------------------------------------------
install.packages("tidyverse")
library(tidyverse)
# Help --------------------------------------------------------------------
?mean
help("mean")
help.search("mea")
# Operators ---------------------------------------------------------------
x <- 10 #user workspace
x = 10 #scope of a function
2 * 4
2 / 4
2 - 4
2 + 4
4 %% 2 #4 mod 2
#boolean
2 == 4
2 != 4
#combinations
(2 == 4) & (3 == 3) #and
(2 == 4) | (3 == 3) #or
2 < 4
4 <= 4
4 > 2
4 >= 4
# Control Flow ------------------------------------------------------------
bool <- TRUE
if (bool) {
print("Hello World")
}
if (!bool) {
print("Hello World")
} else {
print("Pineapple!")
}
#ifelse function
ifelse(!bool, "Hello World", "Pineapple")
#Loops
iter <- 1:5
for (i in iter) {
msg <- paste("Value:", i)
print(msg)
}
msgs <- paste("Value:", 1:5)
for (m in msgs) {
print(m)
}
#while loop
x <- 1
while (x <= 10) {
print(x)
x = x + 1
}
# Defining functions ------------------------------------------------------
my_func <- function(x, y, z) {
msg <- paste("X:", x, "Y:", y, "Z:", z)
print(msg)
}
my_func(1, 2, 3)
# Apply functions ---------------------------------------------------------
#If time allows...