-
Notifications
You must be signed in to change notification settings - Fork 1
/
exercise2.Rmd
201 lines (141 loc) · 2.79 KB
/
exercise2.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
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
## Exercise 2. Numeric vector manipulation (15 minutes)
### Exercise 2a.
Create the script "exercise2.R" and save it to the "Rcourse/Module1" directory: you will save all the commands of exercise 2 in that script.
<br>Remember you can comment the code using #.
**1- Go to Rcourse/Module1
First check where you currently are with getwd();
then go to Rcourse/Module1 with setwd()**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
getwd()
setwd("Rcourse/Module1")
setwd("~/Rcourse/Module1")
```
</details>
**2- Create a numeric vector "y" which contains the numbers from 2 to 11, both included.**
<br>Show y in the console.
<details>
<summary>
*Answer*
</summary>
```{r}
y <- c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
# same as
y <- 2:11
# show in console:
y
```
</details>
**3- How many elements are in y? I.e what is the length of vector y ?**
<details>
<summary>
*Answer*
</summary>
```{r}
length(y)
```
</details>
**4- Show the 2nd element of y.**
<details>
<summary>
*Answer*
</summary>
```{r}
y[2]
```
</details>
**5- Show the 3rd and the 6th elements of y.**
<details>
<summary>
*Answer*
</summary>
```{r}
y[c(3,6)]
```
</details>
**6- Remove the 4th element of y: reassign. What is now the length of y ?**
<details>
<summary>
*Answer*
</summary>
```{r}
# remove 4th element and reassign
y <- y[-4]
# length of y
length(y)
```
</details>
**7- Show all elements of y that are less than 7.**
<details>
<summary>
*Answer*
</summary>
```{r}
# which elements of y are less than 7:
y < 7
# show those elements
y[ y < 7 ]
```
</details>
**8- Show all elements of y that are more than or equal to 4 and less than 9.**
<details>
<summary>
*Answer*
</summary>
```{r}
y[ y >= 4 & y < 9 ]
```
</details>
**9. What are the mean, median, minimum and maximum values of y?**
<details>
<summary>
*Answer*
</summary>
```{r}
mean(y); median(y); min(y); max(y)
```
</details>
**10- Create vector y2 as:**<br>
```{r}
y2 <- c(1, 11, 5, 62, 18, 2, 8)
```
**11- Which elements of y2 are also present in y ?<br>Note: remember the %in% operator.**
<details>
<summary>
*Answer*
</summary>
```{r}
y2[ y2 %in% y ]
```
</details>
### Exercise 2b.
**1- Create the vector myvector as:**
```{r}
myvector <- c(1, 2, 3, 1, 2, 3, 1, 2, 3)
```
**2- Create the same vector using the rep.int() function (look for help: ?rep.int)**
<details>
<summary>
*Answer*
</summary>
```{r}
myvector <- rep.int(x=1:3, times=3)
```
</details>
**3- Calculate the fraction/percentage of each element of myvector (relative to the sum of all elements of the vector).<br>Note: function sum() can be useful...**
<details>
<summary>
*Answer*
</summary>
```{r}
# sum of all elements of the vector
mytotal <- sum(myvector)
# divide each element by the sum
myvector / mytotal
# multiply by 100 to get a percentage
(myvector / mytotal) * 100
```
</details>