## 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 the 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.

- #Creating Vector
- firstVector <- 1:5
- firstVector
- secondVector <- 5:9
- secondVector
- #Adding above two vectors
- VectorAfterAddition <- firstVector + secondVector
- VectorAfterAddition

**Output
**[1] 6 8 10 12 14

**Case 1 - Adding Vectors of Different Length
**When we try to add vectors of different lengths the R studio gives a warning message as the longer object length is not a multiple of the shorter object length.

- #Creating Vector
- firstVector <- 1:5
- firstVector
- secondVector <- 5:10
- secondVector
- #Adding above two vectors
- VectorAfterAddition <- firstVector + secondVector
- VectorAfterAddition

**Output
**[1] 6 8 10 12 14 11

**Case 2 - Adding Vectors of Different DataTypes
**When we try to add two vectors of different types, for example, a vector of integer and string, then it gives an error as Error in firstVector + secondVector non-numeric argument to the binary operator. It is shown in the below image.

- #Creating Vector
- firstVector <- 1:3
- firstVector
- secondVector <- rep("Suraj", 3)
- secondVector
- ResultAfterAddition <- firstVector + secondVector
- ResultAfterAddition

**Output
**Error in firstVector + secondVector,

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

- #Creating Vector
- firstVector <- rep("Suraj", 3)
- firstVector
- secondVector <- rep("Suraj", 3)
- secondVector
- ResultAfterAddition <- firstVector + secondVector
- ResultAfterAddition

**Output
**Error in firstVector + secondVector,

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

- #Creating Vector
- firstVector <- seq(1,2, by=0.2)
- firstVector
- secondVector <- rep(1, 6)
- secondVector
- ResultAfterAddition <- firstVector + secondVector
- ResultAfterAddition

**Output**

## 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.
**

Recommended Free Ebook

Similar Articles