Operations on Vectors in R
Last Updated :
25 Jan, 2022
Vectors are the most basic data types in R. Even a single object created is also stored in the form of a vector. Vectors are nothing but arrays as defined in other languages. Vectors contain a sequence of homogeneous types of data. If mixed values are given then it auto converts the data according to the precedence. There are various operations that can be performed on vectors in R.
Creating a vector
Vectors can be created in many ways as shown in the following example. The most usual is the use of ‘c’ function to combine different elements together.
Python3
X < - c( 1 , 4 , 5 , 2 , 6 , 7 )
print ( 'using c function' )
print (X)
Y < - seq( 1 , 10 , length.out = 5 )
print ( 'using seq() function' )
print (Y)
Z < - 5 : 10
print ( 'using colon' )
print (Y)
|
Output:
using c function 1 4 5 2 6 7
using seq function 1.00 3.25 5.50 7.75 10.00
using colon 5 6 7 8 9 10
Accessing vector elements
Vector elements can be accessed in many ways. The most basic is using the ‘[]’, subscript operator. Following are the ways of accessing Vector elements:
Note: vectors in R are 1 based indexed, unlike the normal C, python, etc format where indexing starts from 0.
Python3
X < - c( 2 , 5 , 8 , 1 , 2 )
print ( 'using Subscript operator' )
print (X[ 2 ])
Y < - c( 4 , 5 , 2 , 1 , 7 )
print ( 'using c function' )
print (Y[c( 4 , 1 )])
Z < - c( 5 , 2 , 1 , 4 , 4 , 3 )
print ( 'Logical indexing' )
print (Z[Z> 3 ])
|
Output:
using Subscript operator 5
using c function 1 4
Logical indexing 5 4 4
Modifying a vector
Vectors can be modified using different indexing variations which are mentioned in the below code:
Python3
X < - c( 2 , 5 , 1 , 7 , 8 , 2 )
X[ 3 ] < - 11
print ( 'Using subscript operator' )
print (X)
X[X> 9 ] < - 0
print ( 'Logical indexing' )
print (X)
X < - X[c( 5 , 2 , 1 )]
print ( 'using c function' )
print (X)
|
Output:
Using subscript operator 2 5 11 7 8 2
Logical indexing 2 5 0 7 8 2
using c function 8 5 2
Deleting a vector
Vectors can be deleted by reassigning them as NULL. To delete a vector we use the NULL operator.
Python3
X < - c( 5 , 2 , 1 , 6 )
X < - NULL
print ( 'Deleted vector' )
print (X)
|
Deleted vector NULL
Arithmetic operations
We can perform arithmetic operations between 2 vectors. These operations are performed element-wise and hence the length of both the vectors should be the same.
Python3
X < - c( 5 , 2 , 5 , 1 , 51 , 2 )
Y < - c( 7 , 9 , 1 , 5 , 2 , 1 )
Z < - X + Y
print ( 'Addition' )
print (Z)
S < - X - Y
print ( 'Subtraction' )
print (S)
M < - X * Y
print ( 'Multiplication' )
print (M)
D < - X / Y
print ( 'Division' )
print (D)
|
Output:
Addition 12 11 6 6 53 3
Subtraction -2 -7 4 -4 49 1
Multiplication 35 18 5 5 102 2
Division 0.7142857 0.2222222 5.0000000 0.2000000 25.5000000 2.0000000
Sorting of Vectors
For sorting we use the sort() function which sorts the vector in ascending order by default.
Python3
X < - c( 5 , 2 , 5 , 1 , 51 , 2 )
A < - sort(X)
print ( 'sorting done in ascending order' )
print (A)
B < - sort(X, decreasing = TRUE)
print ( 'sorting done in descending order' )
print (B)
|
Output:
sorting done in ascending order 1 2 2 5 5 51
sorting done in descending order 51 5 5 2 2 1