-
Notifications
You must be signed in to change notification settings - Fork 6
/
rfe_ch9.R
93 lines (88 loc) · 1.55 KB
/
rfe_ch9.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
# R for Everyone, ch 9, Control Statements
#9.1 If-else
as.numeric(TRUE)
as.numeric(FALSE)
toCheck <- 1
if (toCheck == 1)
{
print("hello")
}
if (toCheck != 1)
{
print("hello")
}
# print nothing
check.bool <- function(x)
{
if (x == 1)
{
print("hello")
} else
{
print("goodbye")
}
}
check.bool(1)
check.bool(0)
check.bool("k")
check.bool(TRUE)
check.bool <- function(x)
{
if (x == 1)
{
print("hello")
} else if (x==0)
{
print("goodbye")
} else
{
print("confused")
}
}
check.bool(1)
check.bool(0)
check.bool(2)
check.bool("k")
#9.2 switch
use.switch <- function(x)
{
switch(x,
"a"="first",
"b"="second",
"z"="last",
"c"="third",
"other")
}
use.switch("a")
use.switch("b")
use.switch("c")
use.switch("d")
use.switch("e")
use.switch("z")
use.switch(1)
use.switch(2)
use.switch(3)
use.switch(4)
use.switch(5)
use.switch(6) #nothing is returned
is.null(use.switch(6))
#9.3 ifelse
ifelse(1 == 1, "Yes", "No")
ifelse(1 == 0, "Yes", "No")
toTest <- c(2,2,1,2,1,2)
ifelse(toTest == 2, "Yes", "No")
ifelse(toTest == 2, toTest * 3, toTest)
ifelse(toTest == 2, toTest * 3, "ONE")
# notice 6 -> "6"
toTest[2] <- NA
toTest
ifelse(toTest == 2, "Yes", "No")
ifelse(toTest == 2, toTest * 3, toTest)
ifelse(toTest == 2, toTest * 3, "ONE")
#9.4 Compound Tests
a <- c(1,1,0,1)
b <- c(2,1,0,1)
ifelse(a==1 & b==1, "Yes","No")
# & compare each element
ifelse(a==1 && b==1, "Yes","No")
# && compare only one element