## 9.1 Vectors

A vector is a sequence of data elements of the **same type**.

329 | 45 | 12 | 28 |

### 9.1.1 Creating a vector

- Values are assigned to a vector using the
**c**function (**c**for**c**ombining elements). Open and close the round brackets, and place the different elements, separated by**commas**.

`<- c(329, 45, 12, 28) vector1 `

You can create an empty vector with:

```
# empty vector
<- vector()
vecempty # empty numerical vector with 5 elements
<- vector(mode="numeric", length=5)
vecnumempty # empty character vector with 8 elements
<- vector(mode="character", length=8) veccharempty
```

- Create a sequence of consecutive numbers:

```
<- 10:16
vecnum # shortcut for:
<- c(10, 11, 12, 13, 14, 15, 16)
vecnum # NOTES: both ends (10 and 16) are included
```

- Character vectors: each element is entered between (single or double) quotes.

mRNA | miRNA | snoRNA | lncRNA |

`<- c("mRNA", "miRNA", "snoRNA", "lncRNA") mynames `

**HANDS-ON**

- Create a vector (choose the name) that contains a sequence of number, from 3 to 8, both included.
- Create vector myfruits as:

`<- c(apple, pear, orange) myfruits `

What is happening? Why? Can you modify the line above so it doesn’t throw an error?

### 9.1.2 Vector manipulation

- A vector can be
**named**: each element of the vector can be assigned a name (number or character)

```
# our previous numerical vector
<- c(329, 45, 12, 28)
vector1 # assign names
names(vector1) <- c("mRNA", "miRNA", "snoRNA", "lncRNA")
# use an object which already contains a vector
<- c("mRNA", "miRNA", "snoRNA", "lncRNA")
mynames names(vector1) <- mynames
```

- Get the length (number of elements) of a vector

`length(vector1)`

`## [1] 4`

- Extracting elements from vector
**a**- extract elements using their position (index) in the vector:

`1] vector1[`

`## mRNA ## 329`

`c(1,3)] vector1[`

`## mRNA snoRNA ## 329 12`

`2:4] vector1[`

`## miRNA snoRNA lncRNA ## 45 12 28`

- extract elements using their names:

`"mRNA"] vector1[`

`## mRNA ## 329`

`c("miRNA", "lncRNA")] vector1[`

`## miRNA lncRNA ## 45 28`

- Reassigning a vector’s element

```
# here we re-assign the 2nd element of vector1 with 31.
2] <- 31
vector1["miRNA"] <- 31 vector1[
```

- Removing a vector’s element

```
# here we remove the 3rd element of vector1
# show in the console vector1 without its 3rd element:
-3] vector1[
```

```
## mRNA miRNA lncRNA
## 329 31 28
```

```
# reassign vector1 without its 3rd element
<- vector1[-3] vector1
```

**Show**versus**change**

x[-2] x unchanged !

x <- x[-2] x reassigned !

### 9.1.3 Combining vectors

- You can combine 2 (or more) vectors into one.
Here, we combine
**v1**and**v2**to create a vector**v3**

```
<- 2:5
v1 <- 4:6
v2 <- c(v1, v2) v3
```

The elements of

v2are added after the elements ofv1

- Likewise, you can add elements at the end of a vector

`<- c(v3, 19) v3 `

### 9.1.4 Numeric vector manipulation

#### Logical operators

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?

```
<- 1:5
a == 2 a
```

`## [1] FALSE TRUE FALSE FALSE FALSE`

- Which elements of
**a**are superior to 2?

```
<- 1:5
a > 2 a
```

`## [1] FALSE FALSE TRUE TRUE TRUE`

- Extract elements of a vector that comply with a condition:

```
<- 1:5
a # extract the TRUE/FALSE vector
>= 2 a
```

`## [1] FALSE TRUE TRUE TRUE TRUE`

```
# extract the actual sub-vector that complies the condition (TRUE values)
>= 2] a[a
```

`## [1] 2 3 4 5`

```
# count how many elements comply the condition
length(a[a >= 2])
```

`## [1] 4`

**HANDS-ON**

Given the following vector containing a group of people’s ages:

`<- c(18, 32, 12, 34, 42, 32, 17, 56, 76, 10) ages `

- How many people are over 18?
- How many people are under 60?
- How many people are between 25 and 45?

#### 9.1.4.1 Operations on vectors

- Adding 2 to a vector adds 2 to
**each element**of the vector:

```
<- 1:5
a + 2 a
```

`## [1] 3 4 5 6 7`

Same goes for subtractions, multiplications and divisions…

- Multiplying a vector by another vector of equal length

```
<- c(2, 4, 6)
a <- c(2, 3, 0)
b * b a
```

`## [1] 4 12 0`

- Multiplying a vector by another
**shorter**vector

```
<- c(2, 4, 6, 3, 1)
a <- c(2, 3, 0)
b * b a
```

`## Warning in a * b: longer object length is not a multiple of shorter object length`

`## [1] 4 12 0 6 3`

Vector

ais “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 |

```
<- c(1, 3, 12, 45, 3, 2)
k summary(k)
```

```
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 2.25 3.00 11.00 9.75 45.00
```

**HANDS-ON**

Given our **ages** vector:

`<- c(18, 32, 12, 34, 42, 32, 17, 56, 76, 10) ages `

- What is the average/mean age?
- What is percentage of variation? It is defined as (standard deviation / mean) * 100.

#### 9.1.4.2 Comparing vectors

- The
**%in%**operator

Which elements of **a** are also found in **b** ?

```
<- 2:6
a <- 4:10
b %in% b a
```

`## [1] FALSE FALSE TRUE TRUE TRUE`

Yields a TRUE/FALSE vector of the size of **a**.

Retrieve actual elements of **a** that are found in **b**:

```
<- 2:6
a <- 4:10
b %in% b] a[a
```

`## [1] 4 5 6`

- Intersection:

Retrieve the intersection of 2 vectors:

`intersect(a, b)`

`## [1] 4 5 6`

- Check if 2 vectors are exactly identical

`identical(a, b)`

`## [1] FALSE`

### 9.1.5 Character vector manipulation

Character vectors are manipulated similarly to numeric ones.

- The
**%in%**operator:

```
<- c("mRNA", "miRNA", "snoRNA", "RNA", "lincRNA")
k <- c("mRNA","lincRNA", "tRNA", "miRNA")
p %in% p k
```

`## [1] TRUE TRUE FALSE FALSE TRUE`

`%in% p] k[k `

`## [1] "mRNA" "miRNA" "lincRNA"`

- Select elements from vector
**m**that are not*exon*

```
<- c("exon", "intron", "exon")
m != "exon" m
```

`## [1] FALSE TRUE FALSE`

`!= "exon"] m[m `

`## [1] "intron"`