-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession2_controlFlow.R
executable file
·107 lines (84 loc) · 2.24 KB
/
session2_controlFlow.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Control flow
# When we're coding, we often want to control operations base on certain conditions, and we may want a certain action to happen repetitively.
# Conditional statement
# if
if (condition is true) {
perform action
}
# if ... else
if (condition is true) {
perform action
} else { # that is, if the condition is false,
perform alternative action
}
# example
x <- 11
if (x < 10) {
print("X is larger than 10")
}
# More conditions
if (x > 10) {
print("X is larger than 10")
} else if (x > 5 & x <= 10) {
print("X is larger than 5 and smaller or equal to 10")
} else {
print("X is smaller than 5")
}
### Repeating operations
# For loop: purpose- repeat a sequence of instructions under certain conditions
#// allow to group the same part of code and automate it
# for better understanding: it can be read as: for each iterator that's \
# in the sequence, you execude the code chunk
for(iterator in set of values){
do a thing
}
## want to print 1,2,3,...10 ; one way:
print(1)
print(2)
print(3)
...
print(10)
# for loop: i will be updated after each iteration;
# read as: for each i that's in the sequence c(1,2,3,...10), you execute the code:
# "print(i)"
for (i in 1:10){
print(i)
}
# Nested for loops
for(i in 1:5){
#i=1, j="a',"b","c"...; i =2, j="a","b"...
for(j in c('a', 'b', 'c', 'd', 'e')){
print(paste(i,j))
}
}
# We can save results of these operations.
result_vector <- c() # initialize empty vector // optional
for(i in 1:5){
for(j in c('a', 'b', 'c', 'd', 'e')){
#print(paste(i,j))
temp_value <- paste(i, j)
result_vector <- c(result_vector, temp_value)
}
}
### R is special! => everything in r is considered as a vector or a combination
# of serveral vectors
# Vectorized operations in R => manipulate vectors as a whole // high efficiency
paste(1:5, c('a', 'b', 'c', 'd', 'e'))
x <- 1:5
y <- 11:15
x + y
# Some tips:
# Use vectorized operations when possible.
# Do not grow objects. Initialize a vector/matrix with fixed size and fill it in.
x <- c()
system.time(
for(i in 1:4000){
x<-c(x,i) #here i is combined with previous contents of x
}
)
x<-numeric(4000) #empty numeric vector
system.time(
for(i in 1:4000){
x[i] <- i #changing value of particular element of x
}
)