Operations on Vectors in R

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Use of 'c' function 
# to combine the values as a vector.
# by default the type will be double
X <- 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:10
print('using colon')
print(Y)

chevron_right


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.



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 indexing
Z <- c(5, 2, 1, 4, 4, 3)
print('Logical indexing')
print(Z[Z>3])

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a vector
X <- c(2, 5, 1, 7, 8, 2)
   
# modify a specific element
X[3] <- 11
print('Using subscript operator')
print(X)
   
# Modify using different logics.
X[X>9] <- 0
print('Logical indexing')
print(X)
   
# Modify by specifying the position or elements.
X <- X[c(5, 2, 1)]
print('using c function')
print(X)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a vector
X <- c(5, 2, 1, 6)
   
# Deleting a vector
X <- NULL
print('Deleted vector')
print(X)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating Vectors
X <- c(5, 2, 5, 1, 51, 2)
Y <- c(7, 9, 1, 5, 2, 1)
   
# Addition
Z <- X + Y
print('Addition')
print(Z)
   
# Subtraction
S <- X - Y
print('Subtraction')
print(S)
   
# Multiplication
M <- X * Y
print('Multiplication')
print(M)
   
# Division
D <- X / Y
print('Division')
print(D)

chevron_right


Output:

Addition 12 11  6  6 53  3
Subtracttion -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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a Vector
X <- c(5, 2, 5, 1, 51, 2)
   
# Sort in ascending order
A <- 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)

chevron_right


Output:

sorting done in ascending order 1  2  2  5  5 51
sorting done in descending order 51  5  5  2  2  1



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.