-
Notifications
You must be signed in to change notification settings - Fork 17
/
p1exercises.qmd
222 lines (167 loc) · 4.21 KB
/
p1exercises.qmd
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Exercises {.unnumbered}
## Questions
::: callout
## Exercise: 5-A
Write a function called "multiply" that accepts two numbers as arguments and outputs the product of those two numbers when called as is demonstrated below.
``` r
multiply(3, 3)
# [1] 9
```
:::
::: callout
## Exercise: 5-B
Write an equation that returns the remainder of 12 divided by 8.
:::
::: callout
## Exercise: 5-C
Write an equation that returns the remainder of 36 divided by 10.
:::
::: callout
## Exercise: 5-D
Write a "while" loop that prints all even numbers from 0 to 10.
It's possible for this task to be accomplished in several ways; however, the output of your program should always look like this:
``` r
# [1] 0
# [1] 2
# [1] 4
# [1] 6
# [1] 8
# [1] 10
```
:::
::: callout
## Exercise: 5-E
You are given a vector that looks like this:
``` r
numbers <- c(0:12)
```
Write a for loop that loops through your vector and prints any element greater than or equal to 3.
It's possible for this task to be accomplished in several ways; however, the output of your program should always look like this:
``` r
# [1] 3
# [1] 4
# [1] 5
# [1] 6
# [1] 7
# [1] 8
# [1] 9
# [1] 10
# [1] 11
# [1] 12
```
:::
::: callout
## Exercise: 6-A
Convert the following character variable to a variable with the data type "raw":
``` r
x <- "Trevor rocks"
```
You should store your raw data in a variable named "raw_data", print the data to the console, and check the data type with the "typeof" function. Your output should look like the following:
``` r
print(raw_data)
# [1] 54 72 65 76 6f 72 20 72 6f 63 6b 73
typeof(raw_data)
# [1] "raw"
```
:::
::: callout
## Exercise: 6-B
Create a variable named "spending" and give it a value of 120. Then create a variable named "budget" and give it a value of 100. Next, check whether spending is greater than budget and store the resulting logical data in a variable named "over_budget". Finally, print the value of "over_budget" variable and check it's data type with the "typeof" function.
Your final output should look like this:
``` r
print(over_budget)
# [1] TRUE
typeof(over_budget)
# [1] "logical"
```
:::
::: callout
## Exercise: 7-A
Create a vector named "animal" and give it the following three values: "cow", "cat", "pig". Create a second vector named "sound" and give it the following three values: "moo", "meow", "oink". Finally, create a data frame named "animal_sounds" and assign each of these vectors to be a column.
After printing the resulting data frame to the console, you should get the following output:
``` r
# animal sound
# 1 cow moo
# 2 cat meow
# 3 pig oink
```
:::
## Answers
::: callout
## Answer: 5-A
One way you could accomplish this task is demonstrated in the following solution.
```{r}
multiply <- function(x, y) {
return (x * y)
}
multiply(3, 3)
```
:::
::: callout
## Answer: 5-B
A remainder is referred to as "modulus" in programming. We can use the "%%" operator to accomplish this. For this example, the output of your equation should be 4.
```{r}
12 %% 8
```
:::
::: callout
## Answer: 5-C
A remainder is referred to as "modulus" in programming. We can use the "%%" operator to accomplish this. For this example, the output of your equation should be 6.
```{r}
36 %% 10
```
:::
::: callout
## Answer: 5-D
Here's one way you could write your `while` loop to achieve this output:
```{r}
i <- 0
while (i <= 10) {
print(i)
i <- i + 2
}
```
:::
::: callout
## Answer: 5-E
Here's one way you could write your `for` loop to achieve this output:
```{r}
numbers <- c(0:12)
for (number in numbers) {
if (number >= 3) {
print(number)
}
}
```
:::
::: callout
## Answer: 6-A
You can accomplish this task with the "charToRaw" function.
```{r}
x <- "Trevor rocks"
raw_data <- charToRaw(x)
print(raw_data)
typeof(raw_data)
```
:::
::: callout
## Answer: 6-B
The following example demonstrates how you can accomplish this task.
```{r}
spending <- 120
budget <- 100
over_budget <- spending > budget
print(over_budget)
typeof(over_budget)
```
:::
::: callout
## Answer: 7-A
The following example demonstrates how you can accomplish this task.
```{r}
animal <- c("cow", "cat", "pig")
sound <- c("moo", "meow", "oink")
animal_sounds <- data.frame(animal = animal, sound = sound)
print(animal_sounds)
```
:::