Write a R Program to Two vector Variables (Addition, Subtraction, Multiplication, Division, Mod, Coefficient)

R Program to Two vector Variables

Vectors in R: In R, a vector is a basic data structure that represents an ordered set of values. Vectors can be of different types, such as numeric, character, logical, etc. The elements of a vector are accessed using indexing, and operations can be performed on entire vectors efficiently.

Two Vector Variables: When we refer to two vector variables, it means having two different vectors, each containing a set of values. These vectors can be of the same or different types, depending on the requirements of the analysis or task.

R Program with Two Vector Variables:

Here’s a simple R program that involves two numeric vectors. It calculates the element-wise sum of the vectors and prints the result

Input:

x <- c(2 ,3 ,4 ,4) 
y <-c(3, 4 ,5 ,6)
print (x+y)
print(x-y)
print(x*y)
print(x/y)
print(x%%y)
print(x^y)
or
sum(x,y)
prod(x,y)

Output:

$Rscript main.r
[1] 5 7 9 10
[1] -1 -1 -1 -2
[1] 6 12 20 24
[1] 0.6666667 0.7500000 0.8000000 0.6666667
[1] 2 3 4 4
[1] 8 81 1024 4096
[1] 31
[1] 34560 

Conclusion:

In conclusion, working with two vector variables in R involves creating and manipulating two vectors. These vectors can represent data points, measurements, or any other relevant information. In the provided R program, two numeric vectors are created, and their element-wise sum is calculated. This is a simple example, but in practice, working with two vector variables allows for a wide range of data analysis and manipulation tasks in R. Understanding how to operate on vectors is fundamental for effective data processing and analysis in the R programming language.

Scroll to Top