R – Vectors

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 :.

> #a is a numeric vector
> a = 1:7
> a
[1] 1 2 3 4 5 6 7

Create R Vector using Sequences

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

> a = seq(5, 7)
> a
[1] 5 6 7
> a = seq(5, 7, 0.5)
> a
[1] 5.0 5.5 6.0 6.5 7.0
> 

Create R Vector using c() function

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

> a = c(14, TRUE, "hello", 'k')
> a
[1] "14" "TRUE" "hello" "k"
>

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,

 a[i] 

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

> a = c(56, 25, 45, 19, 88, 63)
> a[2]
[1] 25
> a[6]
[1] 63

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

> a = c(56, 25, 45, 19, 88, 63)
> a[36]
[1] NA

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

> a = c(56, 25, 45, 19, 88, 63)
> indices = c(1,5,6)
> a[indices]
[1] 56 88 63

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.

> a = c(56, 25, 45, 19, 88, 63)
> negindices = c(-2,-4)
> a[negindices]
[1] 56 45 88 63
>

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.

> a = c(56, 25, 45, 19, 88, 63)
> indices = c(TRUE, TRUE, FALSE, TRUE, FALSE, FALSE)
> a[indices]
[1] 56 25 19

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

> a = c(56, 25, 45, 19, 88, 63)
> b = c(41, 52, 22, 41, 11, 21)
> a + 2
[1] 58 27 47 21 90 65
> a + b
[1] 97 77 67 60 99 84
>

R Vector Subtraction

> a = c(56, 25, 45, 19, 88, 63)
> b = c(41, 52, 22, 41, 11, 21)
> a - 10
[1] 46 15 35 9 78 53
> a - b
[1] 15 -27 23 -22 77 42
>

R Vector Multiplication

> a = c(56, 25, 45, 19, 88, 63)
> b = c(41, 52, 22, 41, 11, 21)
> a * 3
[1] 168 75 135 57 264 189
> a * b
[1] 2296 1300 990 779 968 1323
> 

R Vector Division

> a = c(56, 25, 45, 19, 88, 63)
> b = c(41, 52, 22, 41, 11, 21)
> a / 3
[1] 18.666667 8.333333 15.000000 6.333333 29.333333 21.000000
> a / b
[1] 1.3658537 0.4807692 2.0454545 0.4634146 8.0000000 3.0000000
> 

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.

> a = c(56, 25, 45, 19, 88, 63)
> b = c(4, 6)
> a + b
[1] 60 31 49 25 92 69
>

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.

> a = c(56, 25, 45, 19, 88, 63)
> sort(a)
[1] 19 25 45 56 63 88
> sort(a, decreasing=TRUE)
[1] 88 63 56 45 25 19
>

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.

Follow Us On