-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvector.Rmd
244 lines (172 loc) · 4.23 KB
/
vector.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
## Vectors
A vector is a sequence of data elements from the **same type**.
| 329 | 45 | 12 | 28 |
### Creating a vector
* Values are assigned to a vector using the **c** command (**c**ombining elements).
```{r}
a <- c(329, 45, 12, 28)
```
You can create an empty vector with:
```{r}
vecempty <- vector()
```
* Create a sequence of consecutive numbers:
```{r}
a <- 1:6
# same as:
a <- c(1, 2, 3, 4, 5, 6)
# both ends (1 and 6) are included
```
* Character vectors: Each element is entered between (single or double) quotes.
| mRNA | miRNA | snoRNA | lncRNA |
```{r}
b <- c("mRNA", "miRNA", "snoRNA", "lncRNA")
```
### Vector manipulation
* A vector can be **named**: each element of the vector can be assigned a name (number or character)
```{r}
names(a) <- c("mRNA", "miRNA", "snoRNA", "lncRNA")
# use an object which already contains a vector
names(a) <- b
```
* Get the length (number of elements) of a vector
```{r}
length(a)
```
* Extracting elements from vector **a**
+ extract elements using their position (index) in the vector:
<img src="images/vector_indices.png" width="500">
```{r}
a <- 1:6
a[1]
a[c(1,3)]
a[2:4]
```
+ extract elements using their names:
<img src="images/vector_indices_names.png" width="500">
```{r}
a["mRNA"]
a[c("miRNA", "lncRNA")]
```
* Reassigning a vector's element
```{r}
a[2] <- 31
a["miRNA"] <- 31
```
* Removing a vector's element
```{r}
a <- a[-3]
```
* __Show__ versus __change__
x[-2] <img src="images/red_triangle.jpg" width="40"> x <span style="color:red">unchanged</span> !
x <- x[-2] <img src="images/red_triangle.jpg" width="40"> x <span style="color:red">reassigned</span> !
### Combining vectors
* From 2 vectors **a** and **b** you can create a vector **d**
```{r}
a <- 2:5
b <- 4:6
d <- c(a, b)
```
> The elements of **b** are added after the elements of **a**
* Likewise, you can add elements at the end of a vector
```{r}
d <- c(d, 19)
```
### Numeric vector manipulation
<h4>Logical operators</h4>
| Operator | Description |
|:--------: |:-------------:|
| < | less than |
| <= | less than or equal to |
| > | greater than |
| >= | greater than or equal to|
| == | exactly equal to |
| != | not equal to |
| !x | not x |
| x \| y | x OR y |
| x & y | x AND y |
* Which elements of **a** are equal to 2?
```{r}
a <- 1:5
a == 2
```
<img src="images/vector_equal.png" width="450">
* Which elements of **a** are superior to 2?
```{r}
a <- 1:5
a > 2
```
<img src="images/vector_super.png" width="450">
* Extract elements of a vector that comply with a condition:
```{r}
a <- 1:5
a >= 2
a[a >= 2]
```
<img src="images/vector_subvector.png" width="500">
#### Operations on vectors
* Adding 2 to a vector adds 2 to **each element** of the vector:
```{r}
a <- 1:5
a + 2
```
<img src="images/vector_addition.png" width="350">
> Same goes for subtractions, multiplications and divisions...
* Multiplying a vector by another vector of equal length
```{r}
a <- c(2, 4, 6)
b <- c(2, 3, 0)
a * b
```
<img src="images/vector_multiply.png" width="220">
* Multiplying a vector by another **shorter** vector
```{r}
a <- c(2, 4, 6, 3, 1)
b <- c(2, 3, 0)
a * b
```
<img src="images/vector_multi_recycle.png" width="300">
> Vector **a** is "recycled" !
* Summary statistics
| Function | Description |
|:--------: |:-------------:|
| mean(x) | mean / average |
| median(x) | median |
| min(x) | minimum |
| max(x) | maximum |
| var(x) | variance |
| summary(x) | mean, median, min, max, quartiles |
```{r}
a <- c(1, 3, 12, 45, 3, 2)
summary(a)
```
#### Comparing vectors
* The **%in%** operator
Which elements of **a** are also found in **b* ?
```{r}
a <- 2:6
b <- 4:10
a %in% b
```
<img src="images/vector_in.png" width="350">
Retrieve actual elements of **a** that are found in **b**:
```{r}
a <- 2:6
b <- 4:10
a[a %in% b]
```
### Character vector manipulation
Character vectors are manipulated similarly to numeric ones.
* The **%in%** operator:
```{r}
k <- c("mRNA", "miRNA", "snoRNA", "RNA", "lincRNA")
p <- c("mRNA","lincRNA", "tRNA", "miRNA")
k %in% p
k[k %in% p]
```
* Select elements from vector **m** that are not *exon*
```{r}
m <- c("exon", "intron", "exon")
m != "exon"
m[m != "exon"]
```