forked from DaveSheets-Merrimack/DSE5002_Module3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPairExercise_if_else.Rmd
155 lines (108 loc) · 2.81 KB
/
PairExercise_if_else.Rmd
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
---
title: "PairProgramming_If_else"
author: "HDS"
date: "2024-06-24"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## if-else
Pair programming exercise for DSE5002, on if and else operations
when the test condition in an if statement is met, then the following
code block is run. Otherwise, not
if(condiction)
{
...put some commands here
}
Suppose if x is less than 10, then we want to set y=0.125*x and z to x squared
This looks like
```{R}
y=0
z=0
x=5
if(x<10)
{
y=0.125*x
z=x^2
}
#cat is a print function I am using here, the \n is a linefeed character
# to advance to the next line in the printing
cat("Y is ",y ,"\n")
cat("Z is ",z)
```
Action Required: alter my code so that Y and Z are not altered
#If-else
Suppose that when x<10, we want this calculation to run as above, but for
other x values, we want y=0.25*x, and z=square root of x
We use an else statement
```{R}
y=0
z=0
x=5
if(x<10)
{
y=0.125*x
z=x^2
}else
{
y=0.25*x
z=x^(0.5)
}
#cat is a print function I am using here, the \n is a linefeed character
# to advance to the next line in the printing
cat("Y is ",y ,"\n")
cat("Z is ",z)
```
Action Required:
Verify that this code works for a couple of different values of x
Note
Each if statement can only have one else
We can put if statements inside elses to allow for more possible options
```{R}
x=-1
if(x<0)
{
cat("Negative X value\n")
}else
{if(x%%2==1)
{
cat("X is even\n")
}
else
{
cat("X is odd\n")
}
}
```
Alter this code and verify that it works
#Compound test conditions
Using AND (&) and OR(|)
to decide what to do handle this decision
"I walk back the Starbucks some mornings, and while I like their coffee, the
service is slow and I don't like to wait. So I'll stop for coffee there are 2
or less people in line. But if they have scones in stock, I'll stop if there
are 4 or less people in line"
Set up variables
people_in_line- which is an integer
scones_in_stock-which is a binary or logical variable
What test condition would you need to figure out if I will stop at starbucks?
Set up an if statement that prints out the decision
```{R}
people_in_line=3
scones_in_stock=TRUE
# write your code here
```
#If-else assignment statements
R has the ability to carry out assignments in an if-else operator
we send in a condition and two possible assignments, the first for TRUE, the
second for FALSE
it might look like this
```{R}
x=5
y=ifelse(x<10,0.125*x,0.25*x)
y
```
Action: Verify that this behaves as expected when x is changed
This is not a structure I use much, it does save some time. I tend to use
the less sophisticated approach shown above.