In this article, we will learn what is a vector in R programming? how to create a vector in R? and how to access it? Also, we will learn the modification of the vector, a combination of two vectors, and the deletion of the vector. So, let’s get started!
What is a Vector in R?
R programming is the most important language used in data science, statistical analysis, and scientific research. Also, the R language is used mostly in machine learning and artificial intelligence. Vectors in the R programming language are similar to arrays in the C programming language. Vectors store the multiple elements of the same data type in a single variable. But unlike the array in C programming, vectors indexing in R language starts from ‘1’ and not with ‘0’. We can store numeric as well as characters in vectors.
How to Create a Vector in R?
Multiple ways are used to create the vector in the R language.
1) Single Element Vector
Even when a single element is written in the R language, we can consider it a vector. The length of the vector is considered to be 1 and it can be of any data type.
2) Multiple Element vector
- Using Colon Operator: Using this method we can specify the starting and ending point of the vector. For example:
 2 3 4 5 6 7
- Using Sequence(seq) Operator: Using this method we can define the starting and ending point of the vector along with the incrementing gap. For example:
 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0
- Using c() function: The non-character values are converted into character type if any one of the elements is character. For example:
 "green" "FALSE" "6"
What are the types of vectors in R Language?
The vector in R programming can be of different type depending on the data type of the element it contains. Let us study them one by one below:
1) Numeric Vectors
The vectors containing number elements are called numeric vectors. For example:
 1 2 3
2) Integer Vectors
The vectors containing integer elements are called numeric vectors. For example:
 6 7 8
3) Logical Vectors
The vectors containing logical values i.e TRUE or FALSE elements are called numeric vectors. For example:
 TRUE FALSE TRUE
4) Character Vectors
The vectors containing character elements are called numeric vectors. For example:
 "aa" "bb" "cc"
5) Complex Vectors
The vectors containing complex elements are called numeric vectors. For example:
 12+1i 0+3i 5+4i
How to find the type of R vector?
To find out the type of the R vector, we can use typeof() function. For example:
How to Access a Vector in R?
The Vectors in the R language are the same as arrays in the C language. Therefore, we can access the elements of vectors using the indexes just like arrays. The square brackets are used for indexing. To drop particular elements while accessing vectors negative indexing. Also, while accessing the vectors in R we can use TRUE and FALSE or 0 and 1. For example:
 "Jan" "Mar" "May"
 "Jan" "Mar" "May"
How to Modify a Vector in R?
In the R programming language, we can modify the vector using the assignment operator. Modification of vector can be done in many ways as shown in the below:
2 1 9 5 6 7 8 9
How to Combine Vectors in R?
In R programming, we can combine two vectors using c() function. For example:
 "1" "2" "3" "a" "b" "c"
How to Delete a Vector in R?
Deletion of the vector is the process of deleting all the elements of the vector. This can be done by assigning it to the NULL value. For example:
Vectors are the most basic data structure of R programming. It is the collection of the same data type under one variable. Therefore, in this article, we understood the creation, and deletion of vectors along with different operations used to access the vector, modify the vector, and combine any two given vectors. Now, you can easily create a vector in R.