## 9.1 Vectors

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

 329 45 12 28

### 9.1.1 Creating a vector

• Values are assigned to a vector using the c command (combining elements).
``vector1 <- c(329, 45, 12, 28)``

You can create an empty vector with:

``vecempty <- vector()``
• Create a sequence of consecutive numbers:
``````vecnum <- 10:16
# same as:
vecnum <- c(10, 11, 12, 13, 14, 15, 16)
# both ends (10 and 16) are included``````
• Character vectors: Each element is entered between (single or double) quotes.
 mRNA miRNA snoRNA lncRNA
``mynames <- c("mRNA", "miRNA", "snoRNA", "lncRNA")``

### 9.1.2 Vector manipulation

• A vector can be named: each element of the vector can be assigned a name (number or character)
``````names(vector1) <- c("mRNA", "miRNA", "snoRNA", "lncRNA")
# use an object which already contains a vector
names(vector1) <- mynames``````
• Get the length (number of elements) of a vector
``length(vector1)``
``##  4``
• Extracting elements from vector a
• extract elements using their position (index) in the vector: ``````vecnum <- 10:16
vecnum``````
``##  10``
``vecnum[c(1,3)]``
``##  10 12``
``vecnum[2:4]``
``##  11 12 13``
• extract elements using their names: ``vector1["mRNA"]``
``````## mRNA
##  329``````
``vector1[c("miRNA", "lncRNA")]            ``
``````##  miRNA lncRNA
##     45     28``````
• Reassigning a vector’s element
``````vector1 <- 31
vector1["miRNA"] <- 31``````
• Removing a vector’s element
``vector1 <- vector1[-3]``
• Show versus change

x[-2] x unchanged !

x <- x[-2] x reassigned !

### 9.1.3 Combining vectors

• From 2 vectors a and b you can create a vector d
``````v1 <- 2:5
v2 <- 4:6
v3 <- c(v1, v2)``````

The elements of v2 are added after the elements of v1

• Likewise, you can add elements at the end of a vector
``v3 <- c(v3, 19)``

### 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?
``````a <- 1:5
a == 2``````
``##  FALSE  TRUE FALSE FALSE FALSE`` • Which elements of a are superior to 2?
``````a <- 1:5
a > 2``````
``##  FALSE FALSE  TRUE  TRUE  TRUE`` • Extract elements of a vector that comply with a condition:
``````a <- 1:5
a >= 2``````
``##  FALSE  TRUE  TRUE  TRUE  TRUE``
``a[a >= 2]``
``##  2 3 4 5`` #### 9.1.4.1 Operations on vectors

• Adding 2 to a vector adds 2 to each element of the vector:
``````a <- 1:5
a + 2``````
``##  3 4 5 6 7`` Same goes for subtractions, multiplications and divisions…

• Multiplying a vector by another vector of equal length
``````a <- c(2, 4, 6)
b <- c(2, 3, 0)
a * b``````
``##   4 12  0`` • Multiplying a vector by another shorter vector
``````a <- c(2, 4, 6, 3, 1)
b <- c(2, 3, 0)
a * b``````
``````## Warning in a * b: longer object length is not a multiple of shorter object
## length``````
``##   4 12  0  6  3`` 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
``````a <- c(1, 3, 12, 45, 3, 2)
summary(a)``````
``````##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##    1.00    2.25    3.00   11.00    9.75   45.00``````

#### 9.1.4.2 Comparing vectors

• The %in% operator

Which elements of a are also found in b ?

``````a <- 2:6
b <- 4:10
a %in% b``````
``##  FALSE FALSE  TRUE  TRUE  TRUE`` Retrieve actual elements of a that are found in b:

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

### 9.1.5 Character vector manipulation

Character vectors are manipulated similarly to numeric ones.

• The %in% operator:
``````k <- c("mRNA", "miRNA", "snoRNA", "RNA", "lincRNA")
p <- c("mRNA","lincRNA", "tRNA", "miRNA")
k %in% p``````
``##   TRUE  TRUE FALSE FALSE  TRUE``
``k[k %in% p]``
``##  "mRNA"    "miRNA"   "lincRNA"``
• Select elements from vector m that are not exon
``````m <- c("exon", "intron", "exon")
m != "exon"``````
``##  FALSE  TRUE FALSE``
``m[m != "exon"]``
``##  "intron"``