R Vector is a fixed length collection of similar type of elements.

A vector in R programming is one-dimensional.

A vector in R language can be compared to a one-dimensional array in other programming languages like C, Java, etc.

An R Vector can contain elements belonging to one of these types: logical, integer, double, complex, character and raw.

Create R Vector

An R Vector can contain one or more elements.

To create an R Vector, we can use colon operator, sequence, or c() function.

Create R Vector using Colon Operator

In this example, we will create a vector using colon operator :.

Create R Vector using Sequences

In this example we will create a vector using sequences function seq().

Create R Vector using c() function

The c() function coerces non-character values to character type if one of the elements is a character.

Accessing R Vector Elements

Elements of an R Vector can be access using index of the elements. Unlike an array, the index of an R Vector starts at 1, not 0.

Also, multiple elements of a vector can be accessed by passing a vector or indices as an index.

The syntax to access a vector a at index i is,

In the following example, we a simple vector of numbers and we will access elements at some positions.

If you try to access elements outside of the index range, you will get the value as NA.

Now, we will try to access multiple elements of a vector using a vector of indices. The returned value is also a vector.

In the above code, we provided indices 1,5,6 as a vector and a[c(1,5,6)] fetched the elements present at those indices as a vector.

We can also make some elements disappear in the returned vector. All you have to do is, give the indices of those elements with a negative sign.

The elements at positions 2 and 4 are dropped and the resulting vector is returned.

You can also provide TRUE and FALSE in an indices vector. When there is TRUE, the element is considered in the result and when there is FALSE, the element is not considered in the result.

R Vector Arithmetic Operations

You can perform arithmetic operations on vectors. Arithmetic operations like addition, subtraction, multiplication and division. Just remember that the two vectors should be of same length and same type or the second vector can be an atomic value of same type.

For the two vectors with length greater than one, the arithmetic operations are performed like a dot operation (one to one).

R Vector Addition

R Vector Subtraction

R Vector Multiplication

R Vector Division

R Vector Element Recycling

In the above section, we have mentioned that the length of the vectors should be same. If the second vector is of different length, something called recycling happens to the smallest vector to match the size of the largest vector.

In the above example, b becomes c(4,6,4,6,4,6) to match the size of a.

Sorting R Vector

You can sort elements in an R vector using sort() function. The default sorting order is increasing. If you would like to sort in decreasing order, set the argument decreasing to TRUE as shown in the below example.

Concluding this R Tutorial, we have leanred how to create an R Vector, how to perform arithmetic operations on vectors, what is element recycling in R Vector and also sorting an R Vector.