# Operations on Vectors in R

Vectors are the most basic data types in R. Even a single object created is also stored in the form of a vector. Vectors are nothing but arrays as defined in other languages. Vectors contain a sequence of homogeneous types of data. If mixed values are given then it auto converts the data according to the precedence. There are various operations that can be performed on vectors in R.

#### Creating a vector

Vectors can be created in many ways as shown in the following example. The most usual is the use of ‘c’ function to combine different elements together.

`# Use of 'c' function ` `# to combine the values as a vector.` `# by default the type will be double` `X <` `-` `c(` `1` `, ` `4` `, ` `5` `, ` `2` `, ` `6` `, ` `7` `) ` `print` `(` `'using c function'` `)` `print` `(X)` ` ` `# using the seq() function to generate` `# a sequence of continuous values ` `# with different step-size and length.` `# length.out defines the length of vector.` `Y <` `-` `seq(` `1` `, ` `10` `, length.out ` `=` `5` `) ` `print` `(` `'using seq() function'` `) ` `print` `(Y)` ` ` `# using ':' operator to create` `# a vector of continuous values.` `Z <` `-` `5` `:` `10` `print` `(` `'using colon'` `)` `print` `(Y)` |

**Output:**

using c function 1 4 5 2 6 7 using seq function 1.00 3.25 5.50 7.75 10.00 using colon 5 6 7 8 9 10

#### Accessing vector elements

Vector elements can be accessed in many ways. The most basic is using the ‘[]’, subscript operator. Following are the ways of accessing Vector elements:

Note:vectors in R are 1 based indexed, unlike the normal C, python, etc format where indexing starts from 0.

`# Accessing elements using the position number.` `X <` `-` `c(` `2` `, ` `5` `, ` `8` `, ` `1` `, ` `2` `)` `print` `(` `'using Subscript operator'` `)` `print` `(X[` `2` `])` ` ` `# Accessing specific values by passing` `# a vector inside another vector.` `Y <` `-` `c(` `4` `, ` `5` `, ` `2` `, ` `1` `, ` `7` `)` `print` `(` `'using c function'` `)` `print` `(Y[c(` `4` `, ` `1` `)])` ` ` `# Logical indexing` `Z <` `-` `c(` `5` `, ` `2` `, ` `1` `, ` `4` `, ` `4` `, ` `3` `)` `print` `(` `'Logical indexing'` `)` `print` `(Z[Z>` `3` `])` |

**Output:**

using Subscript operator 5 using c function 1 4 Logical indexing 5 4 4

#### Modifying a vector

Vectors can be modified using different indexing variations which are mentioned in the below code:

`# Creating a vector` `X <` `-` `c(` `2` `, ` `5` `, ` `1` `, ` `7` `, ` `8` `, ` `2` `)` ` ` `# modify a specific element` `X[` `3` `] <` `-` `11` `print` `(` `'Using subscript operator'` `)` `print` `(X)` ` ` `# Modify using different logics.` `X[X>` `9` `] <` `-` `0` `print` `(` `'Logical indexing'` `)` `print` `(X)` ` ` `# Modify by specifying the position or elements.` `X <` `-` `X[c(` `5` `, ` `2` `, ` `1` `)]` `print` `(` `'using c function'` `)` `print` `(X)` |

**Output:**

Using subscript operator 2 5 11 7 8 2 Logical indexing 2 5 0 7 8 2 using c function 8 5 2

#### Deleting a vector

Vectors can be deleted by reassigning them as NULL. To delete a vector we use the NULL operator.

`# Creating a vector` `X <` `-` `c(` `5` `, ` `2` `, ` `1` `, ` `6` `)` ` ` `# Deleting a vector` `X <` `-` `NULL` `print` `(` `'Deleted vector'` `)` `print` `(X)` |

Deleted vector NULL

#### Arithmetic operations

We can perform arithmetic operations between 2 vectors. These operations are performed element-wise and hence the length of both the vectors should be the same.

`# Creating Vectors` `X <` `-` `c(` `5` `, ` `2` `, ` `5` `, ` `1` `, ` `51` `, ` `2` `)` `Y <` `-` `c(` `7` `, ` `9` `, ` `1` `, ` `5` `, ` `2` `, ` `1` `)` ` ` `# Addition` `Z <` `-` `X ` `+` `Y` `print` `(` `'Addition'` `)` `print` `(Z)` ` ` `# Subtraction` `S <` `-` `X ` `-` `Y` `print` `(` `'Subtraction'` `)` `print` `(S)` ` ` `# Multiplication` `M <` `-` `X ` `*` `Y` `print` `(` `'Multiplication'` `)` `print` `(M)` ` ` `# Division` `D <` `-` `X ` `/` `Y` `print` `(` `'Division'` `)` `print` `(D)` |

**Output:**

Addition 12 11 6 6 53 3 Subtracttion -2 -7 4 -4 49 1 Multiplication 35 18 5 5 102 2 Division 0.7142857 0.2222222 5.0000000 0.2000000 25.5000000 2.0000000

#### Sorting of Vectors

For sorting we use the ** sort()** function which sorts the vector in ascending order by default.

`# Creating a Vector` `X <` `-` `c(` `5` `, ` `2` `, ` `5` `, ` `1` `, ` `51` `, ` `2` `)` ` ` `# Sort in ascending order` `A <` `-` `sort(X)` `print` `(` `'sorting done in ascending order'` `)` `print` `(A)` ` ` `# sort in descending order.` `B <` `-` `sort(X, decreasing ` `=` `TRUE)` `print` `(` `'sorting done in descending order'` `)` `print` `(B)` |

**Output:**

sorting done in ascending order 1 2 2 5 5 51 sorting done in descending order 51 5 5 2 2 1