-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathclass9.Rmd
142 lines (106 loc) · 2.76 KB
/
class9.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
---
title: 'Data Analysis 3: Week 9'
author: "Alexey Bessudnov"
date: "14 March 2019"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(cache = TRUE)
```
Plan for today:
1. Final statistical report.
2. Assignment 4: solutions.
3. Exercises on conditional statements and iteration.
Exercises.
1. Check if a number is even. If it is print "Even". If it isn't print "Odd". If it's 0 print "Zero".
```{r}
x <- 3
if (x == 0) {
print("Zero")
} else {
if (x %% 2 == 0) {
print("Even")
} else {
print("Odd")
}
}
```
2. Write a for loop finding the largest element of a numeric vector and print it on the screen.
```{r}
x <- c(3, 5, 8, -2)
max(x)
for (i in 1:3) {
print(i)
}
for(i in 1:length(x)) {
print(x[i])
}
max_x <- x[1]
for (i in 2:length(x)) {
if (x[i] > max_x) {max_x <- x[i]}
}
max_x
```
3. Write a for loop finding the largest element of a numeric matrix and print it on the screen.
```{r}
x <- matrix(c(1, -3, 7, 4, 10, 7, -5, 0), nrow = 4)
x
max_x <- x[1, 1]
for (i in 1:dim(x)[1]) {
for (j in 1:dim(x)[2]) {
if (x[i,j] > max_x) {max_x <- x[i,j]}
}
}
max_x
```
4. Write a while loop finding the largest element of a numeric vector and print it on the screen.
```{r}
i <- 1
while (i < 10) {
print(i)
i <- i + 1
}
x <- c(3, 5, 8, -2)
max_x <- x[1]
i <- 2
while (i <= length(x)) {
if (x[i] > max_x) {max_x <- x[i]}
i <- i + 1
}
max_x
```
5. x is a vector with whole numbers (zero and positive integers). If the largest even element of x is smaller than the largest odd element of x, all even elements of x are replaced by 0s. Otherwise all odd elements of x are replaced by 0s. For example, if x = {7; 1; 3; 2; 14; 5; 9; 6} the output should be [0; 0; 0; 2; 14; 0; 0; 6].
```{r}
x <- c(7, 1, 3, 2, 14, 5, 9, 6)
max <- 0
for (i in seq_along(x)) {
if (x[i] > max) {max <- x[i]}
}
for (i in seq_along(x)) {
if (max %% 2 == 0) {
if (x[i] %% 2 != 0) {x[i] <- 0}
}
else {
if (x[i] %% 2 == 0) {x[i] <- 0}
}
}
x
```
6. x is a vector with whole numbers. Write a programme that count the number of pairs of the elements of x where the sum can be divided by 12 without a remainder.
For example, for x = {8; 10; 14; 7; 13; 5; 30; 9; 6} then the answer is 3 ((10, 14); (7, 5); (30, 6)).
```{r}
x <- c(8, 10, 14, 7, 13, 5, 30, 9, 6)
k <- 0
for (i in 1:(length(x)-1)) {
for (j in (i+1):length(x)) {
if ((x[i] + x[j]) %% 12 == 0) {
k <- k + 1
print(c(x[i], x[j]))
}
}
}
k
```