Arithmetic Operation On Vector In R - Adding Vectors In R With Example

In this article we shall learn how to perform arithmetic operation addition on vector in R language

Introduction

R is an important programming language which is used by statisticians. It is also used in machine learning, data science, research, and many more new fields. In a previous article we learned about vector in R, now we shall learn about arithmetic operations; i.e., the addition of two vectors in R. We shall see how it works in R studio.

To know more about vector in R you can visit the link Vector in R

Vector Operation in R

As we know vector in R is a data element so we can perform arithmetic operations on vector in R such as addition, subtraction and multiplication.

Let's start with addition of two vectors in R using R studio.

Addition - Adding Vectors in R

We can add two or more vectors and can get the result as desired. Let's see how it works.
  1. #Creating Vector  
  2. firstVector <- 1:5  
  3. firstVector  
  4. secondVector <- 5:9  
  5. secondVector  
  6.    
  7. #Adding above two vectors  
  8. VectorAfterAddition <- firstVector + secondVector  
  9. VectorAfterAddition  
Output

[1] 6 8 10 12 14

Here we have created two vectors which are firstVector and secondVector. After creating both vectors we have created third vector VectorAfterAddition which is the addition of two vectors created earlier. We can see the result as in the below image, and  clearly both vector elements have been added.

Vector in R 

Now we shall see different cases while adding vectors in R.

Case 1 - Adding Vectors of Different Length

When we try to add vectors of different length the R studio gives a warning message as the longer object length is not a multiple of the shorter object length.

Let's see how it works.

First we shall create two vectors, and after that we shall create another vector with the addition of these two vectors.
  1. #Creating Vector  
  2. firstVector <- 1:5  
  3. firstVector  
  4. secondVector <- 5:10  
  5. secondVector  
  6.   
  7. #Adding above two vectors  
  8. VectorAfterAddition <- firstVector + secondVector  
  9. VectorAfterAddition  
Output

[1] 6 8 10 12 14 11 

Vector in R 

As we can see above in output and image even after giving the warning message R has been added two vectors of different length. So, we can see that it has added elements of the same index and repeated elements of shorter length of the vector while adding. For clarification see the below image.

Remember index for vector in R always starts with 1 not 0. 

Vector in R 

Case 2 - Adding Vectors of Different DataTypes

When we try to add two vectors of different types, for example, vector of integer and string, then it gives an error as Error in firstVector + secondVector non-numeric argument to binary operator. It is shown in the below image.
  1. #Creating Vector  
  2. firstVector <- 1:3  
  3. firstVector  
  4. secondVector <- rep("Suraj", 3)  
  5. secondVector  
  6.    
  7. ResultAfterAddition <- firstVector + secondVector  
  8. ResultAfterAddition  
Output

Error in firstVector + secondVector,

Non-numeric argument to binary operator

Vector in R 

Case 3 - Adding Two Vectors of DataType Character

When we try to add vectors of string then it gives the same error as it has given in the case of adding vectors of integer and string datatypes. This is shown below.
  1. #Creating Vector  
  2. firstVector <- rep("Suraj", 3)  
  3. firstVector  
  4. secondVector <- rep("Suraj", 3)  
  5. secondVector  
  6. ResultAfterAddition <- firstVector + secondVector  
  7. ResultAfterAddition  
Output

Error in firstVector + secondVector,

Non-numeric argument to binary operator

Vector in R

Case 4 - Adding Two Vectors of DataType Decimal and Integer

We can add vectors of decimal and integer type in R. It gives neither a warning nor an error. This is shown below.
  1. #Creating Vector  
  2. firstVector <- seq(1,2, by=0.2)  
  3. firstVector  
  4. secondVector <- rep(1, 6)  
  5. secondVector  
  6. ResultAfterAddition <- firstVector + secondVector  
  7. ResultAfterAddition  
Output

[1] 2.0 2.2 2.4 2.6 2.8 3.0

Vector in R 

Summary

In this article, we have learned how to perform arithmetic operations on vector; i.e., the addition of two vectors in R. We have seen different cases while adding two vectors in R also.

I hope you have learned and enjoyed reading this article. You’re welcome to like, comment, share and leave any type of suggestion for my appreciation.