Operations on Vectors in R - GeeksforGeeks

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.  
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
# Use of 'c' function# to combine the values as a vector.# by default the type will be doubleX <- c(1, 4, 5, 2, 6, 7)print('using c function')print(X)  # using the seq() function to generate# a sequence of continuous values# with different step-size and length.# length.out defines the length of vector.Y <- seq(1, 10, length.out = 5)print('using seq() function')print(Y)  # using ':' operator to create# a vector of continuous values.Z <- 5:10print('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
 
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
# Accessing elements using the position number.X <- c(2, 5, 8, 1, 2)print('using Subscript operator')print(X[2])  # Accessing specific values by passing# a vector inside another vector.Y <- c(4, 5, 2, 1, 7)print('using c function')print(Y[c(4, 1)])  # Logical indexingZ <- 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
 
Vectors can be modified using different indexing variations which are mentioned in the below code:  
Python3
# Creating a vectorX <- c(2, 5, 1, 7, 8, 2)  # modify a specific elementX[3] <- 11print('Using subscript operator')print(X)  # Modify using different logics.X[X>9] <- 0print('Logical indexing')print(X)  # Modify by specifying the position or elements.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
 
Vectors can be deleted by reassigning them as NULL. To delete a vector we use the NULL operator.  
Python3
# Creating a vectorX <- c(5, 2, 1, 6)  # Deleting a vectorX <- NULLprint('Deleted vector')print(X)
Deleted vector NULL
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
# Creating VectorsX <- c(5, 2, 5, 1, 51, 2)Y <- c(7, 9, 1, 5, 2, 1)  # AdditionZ <- X + Yprint('Addition')print(Z)  # SubtractionS <- X - Yprint('Subtraction')print(S)  # MultiplicationM <- X * Yprint('Multiplication')print(M)  # DivisionD <- X / Yprint('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
 
For sorting we use the sort() function which sorts the vector in ascending order by default.  
Python3
# Creating a VectorX <- c(5, 2, 5, 1, 51, 2)  # Sort in ascending orderA <- sort(X)print('sorting done in ascending order')print(A)  # sort in descending order.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
 
sumitgumber28
Picked
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Filter data by multiple conditions in R using Dplyr
Loops in R (for, while, repeat)
Change Color of Bars in Barchart using ggplot2 in R
How to change Row Names of DataFrame in R ?
How to Change Axis Scales in R Plots?
Remove rows with NA in one column of R DataFrame
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
K-Means Clustering in R Programming