Vector Operations in R

Vector Operations in R

  • R
  • 4 mins read

In this article, we will study the various operations that are associated with vectors in R

There is a list of manipulations that can be done with vectors, some of which are briefly described.

Accessing a sequence of elements in the vector

Single, as well as multiple elements, can be accessed from the vector using the indexing operator specified in the [ ]  bracket. In case, a colon (:) is used, the elements are retrieved from the starting position to the ending position. Below is an example of vector operations in R:

#declaring vector in R
vec1 = c(2,5,-1,8,10)

#printing the value of vector
cat("Initial vec : ",vec1)

subset <- vec1[2:4]
cat("Vectors from 2 to 4 position : ", subset)
The code produces the following output : 
Initial vec :  2 5 -1 8 10 
Vectors from 2 to 4 position :  5 -1 8

Modifying the value of a vector 

We already studied indexing the elements of a vector in the previous post. The elements can be indexed by specifying the position which needs to be retrieved and then its value can be manipulated. Here is an example of a vector editing operation in R:

#declaring vector
vec1 = c(2,5,-1)

#printing the value of vector
cat("Initial vec : ",vec1)
#replacing the second element of the vector
vec1[2] <- -4
#printing the value of vector
cat("Final vec : ",vec1)
The output of the code is as follows : 
Initial vec :  2 5 -1
Final vec :  2 -4 -1

Deleting a vector in R

A vector can be deleted by assigning it to the NULL value. The instance of that particular vector is removed from memory. Here is a delete vector operation in R example:

#declaring vector
vec1 = c(2,5)

#printing the value of vector
cat("Initial vec : ",vec1)

#deleting the vector
vec1 <- NULL
#printing the value of vector
cat("Final vec : ",vec1)
The code produces the following output : 
Initial vec :  2 5 
Final vec :

Arithmetic operations on vector in R

Vector elements can be subjected to arithmetic operations, such as addition, multiplication, subtraction, and division, wherein, the corresponding elements of the operands are evaluated. The elements of the vector may be integers, complex numbers, or floating point numbers. The below code snippet illustrates the usage of arithmetic operations over vector elements in R. 

#declaring two vectors
vec1 = c(2,5)
vec2 = c(1,-1)

#adding the components of the vectors 
res1 <- vec1 +vec2
cat("Addition : ", res1)

#subtraction of vectors
res2 <- vec1 - vec2
cat("Subtraction : ",res2)

#multiplication of vectors
res3 <- vec1 * vec2
cat("Multiplication : ",res3)

#division of vectors
res4 <- vec1 / vec2
cat("Division : ",res4)

#modulo of vectors
res5 <- vec1 %% vec2
cat("Modulo : ", res5)

#power of vectors
res6 <- vec1 ^ vec2
cat("Power : ", res6)
The code produces the following output : 
Addition :  3 4
Subtraction :  1 6
Multiplication :  2 -5 
Division :  2 -5 
Modulo :  0 0
Power :  2 0.2

Sorting a vector in R

The values in the vector can be sorted by using the in-built sort() method in R. By default, it sorts the elements in increasing order. In the case of a character vector, the sorting is done lexicographically. Here is a sorting vector operation example in R:

#declaring a vector
vec <- c("foxinfotech","r","oracle","machinelearning")
#printing original vector
cat("Vec : ", vec)
#sorting
sort_vec <- sort(vec)
cat("Sorted vector : ",sort_vec)
Output : 
Vec :  foxinfotech r oracle machinelearning
Sorted vector :  foxinfotech machinelearning oracle r

Combining vectors

Multiple vector objects can also be combined into a single vector by using the c() method, which takes as input the vectors to be combined. Elements are concatenated in the order of their appearance in the vector. In case the input vectors belong to different data types, the resultant object is a list and not an atomic vector. Here is a combining vector operation example in R:

#declaring vector
vec1 = c(2,5,-1,8,10)
vec2 = c("Hi","There")

#combining vectors
comb_vec <- c(vec1,vec2)
cat("Combined Vector : ", comb_vec)
Output produced by the code is : 
Combined Vector :  2 5 -1 8 10 Hi There