forked from daroczig/CEU-R-intro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-extra.R
More file actions
62 lines (53 loc) · 1.42 KB
/
1-extra.R
File metadata and controls
62 lines (53 loc) · 1.42 KB
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
## #############################################################################
## a techie intro into repetitive tasks
## #############################################################################
for (counter in 1:5) {
print(counter)
}
## looping
set.seed(9)
plot(cumsum(round(runif(25))*2 - 1), type = 's')
plot(cumsum(round(runif(25))*2 - 1), type = 's', ylim = c(-10, 10))
for (counter in 2:5) {
lines(cumsum(round(runif(25))*2 - 1), type = 's', col = counter)
}
set.seed(9)
sum(round(runif(25))*2 - 1)
sum(round(runif(25))*2 - 1)
sum(round(runif(25))*2 - 1)
sum(round(runif(25))*2 - 1)
sum(round(runif(25))*2 - 1)
## TODO check the distribution of values after the 25th iteration
set.seed(9)
x <- sum(round(runif(25))*2 - 1)
for (i in 1:50) { # 1000
x <- c(x, sum(round(runif(25))*2 - 1))
}
hist(x)
## rings a bell?
hist(replicate(1000, sum(round(runif(25))*2 - 1)))
## do something until ...
set.seed(9)
x <- sum(round(runif(25))*2 - 1)
for (i in 1:5000) {
if (x > 20) {
print('found it!')
break()
}
x <- sum(round(runif(25))*2 - 1)
}
## increase x to 15
## increase x to 20 -> increase counter -> Inf -> while?
set.seed(9)
x <- sum(round(runif(25))*2 - 1)
while (x < 20) {
x <- sum(round(runif(25))*2 - 1)
}
set.seed(9)
x <- sum(round(runif(25))*2 - 1)
counter <- 0
while (x < 20) {
counter <- counter + 1
x <- sum(round(runif(25))*2 - 1)
}
## we know for loops and if conditions!