How to Add Vectors in R Programming

A vector in R is a sequence of data elements of the same data type. To create a Vector in R, we generally use the c() function. Since vector contains data elements, we can perform any arithmetic operations, such as addition, subtraction, multiplication, and division. The arithmetic operations will be performed on a member-by-member basis in a vector.

How to Add Vectors in R

To add vectors in R, use the + operator. While adding vectors, you need to take care of the recycling rule. If the two vectors are of equal length, then there is no problem. But if the vectors’ lengths are different, then the shorter one is repeated until its length is equal to that of the longer one.

R Program to add two vectors.

To add two vectors, use the + operator.

fv <- 1:4
cat("The first vector is: ", fv, "\n")
sv <- 5:8
cat("The second vector is: ", sv, "\n")

add <- fv + sv
cat("The final vector is: ", add)

Output

The first vector is: 1 2 3 4
The second vector is: 5 6 7 8
The final vector is: 6 8 10 12

Here, we have added fv and sv together; the sum would be the fv vector whose members are the sum of the corresponding members from fv and sv.

You can see that fv and sv have the same vector length to be added without difficulty.

Adding Vectors of Different Length

What if two vectors have a different length? Then we will get a warning message saying: longer object length is not a multiple of shorter object length.

For example, let’s take the length of fv to 4 and sv to 8 and add the two vectors.

fv <- 1:4
cat("The first vector is: ", fv, "\n")
sv <- 5:13
cat("The second vector is: ", sv, "\n")

add <- fv + sv
cat("The final vector is: ", add)

Output

The first vector is: 1 2 3 4
The second vector is: 5 6 7 8 9 10 11 12 13
Warning message:
In fv + sv :
longer object length is not a multiple of shorter object length
The final vector is: 6 8 10 12 10 12 14 16 14

You can see that the addition takes place until both vectors have the same length; after that, the longer length vector’s elements are added as it is to the final sum vector.

Adding Vectors of Different DataTypes

If 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 fv + sv non-numeric argument to the binary operator.

fv <- 1:4
cat("The first vector is: ", fv, "\n")
sv <- c("Snape", "Minerva", "Horace", "Lupin")
cat("The second vector is: ", sv, "\n")

add <- fv + sv
cat("The final vector is: ", add)

Output

The first vector is: 1 2 3 4
The second vector is: Snape Minerva Horace Lupin
Error in fv + sv : non-numeric argument to binary operator
Execution halted

As expected, we got non-numeric argument to binary operatorerror.

Adding Two Vectors of DataType Character

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

dv <- rep("Grogu", 3)
cat("The first vector is: ", dv, "\n")
iv <- rep("Grogu", 3)
cat("The second vector is: ", iv, "\n")
add <- dv + iv
print(add)
cat("The final vector is: ", add)

Output

The first vector is: MJ MJ MJ
The second vector is: MJ MJ MJ
Error in dv + iv : non-numeric argument to binary operator
Execution halted

And we get the same error: non-numeric argument to the binary operator.

Adding Two Vectors of DataType Decimal and Integer

If we add decimal and integer vectors, then it won’t give us an error message.

dv <- seq(1, 2, by = 0.2)
cat("The first vector is: ", dv, "\n")
iv <- rep(1, 6)
cat("The second vector is: ", iv, "\n")
add <- dv + iv
print(add)
cat("The final vector is: ", add)

Output

The first vector is: 1 1.2 1.4 1.6 1.8 2
The second vector is: 1 1 1 1 1 1
[1] 2.0 2.2 2.4 2.6 2.8 3.0
The final vector is: 2 2.2 2.4 2.6 2.8 3

To create the first vector, we used the seq() function, which generates a sequence.

To create the second vector, we used the rep() function.

Conclusion

Adding vectors in R is a common operation, and there are lots of things that matter when you add two vectors. You can add vectors of the same length or different lengths. You can add vectors of integer and decimal data types, but you can’t add vectors of two completely different data types like string and integer.

Leave a Comment