-
Notifications
You must be signed in to change notification settings - Fork 6
/
rfe_ch10.R
62 lines (54 loc) · 973 Bytes
/
rfe_ch10.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
# R for Everyone, ch 10, Loops, the Un-R Way to Iterate
#10.1 for
for (i in 1:10) {
print(i)
}
print(1:10)
fruit <- c("apple","banana","pomegranate")
(fruitLength <- rep(NA, length(fruit)))
names(fruitLength)
names(fruitLength) <- fruit
fruitLength
for (a in fruit) {
fruitLength[a] <- nchar(a)
}
fruitLength
# use R's build in vectorization is much easier
fruitLength2 <- nchar(fruit)
names(fruitLength2) <- fruit
fruitLength2
identical(fruitLength,fruitLength2)
#10.2 while
x <- 1
while (x <= 5) {
print(x)
x <- x + 1
}
#10.3 Controlling Loops
for (i in 1:10) {
if (i==3)
{
next
}
print(i)
}
for (i in 1:10) {
if (i==4)
{
break
}
print(i)
}
#compare running time
g <- rnorm(1000000)
h <- rep(NA, 1000000)
#start the clock
ptm <- proc.time()
for (i in 1:1000000) {
h[i] <- g[i] + 1
}
#stop the clock
proc.time() - ptm
ptm <- proc.time()
h <- g+1
proc.time() - ptm