Vector is a sequence of data elements of the same type. It is a one dimensional array. Members of a vector are known as components.

There are five classes of vectors:

• Numeric
• Integer
• Complex
• Logical
• Character

Creating a Vector

Single element vector

In R, when you write just one value, it is considered as a vector of length 1. Let’s see the different type of vectors with a single element:

Output:

Multiple elements vector

We can create a vector of consecutive numbers through the:

1. colon operator (:)

Example:

2. Using sequence operator (seq.)

Example:

Output:

3. Using the c() function

Example:

Output:

Accessing vector elements

We can access the elements of a vector with the help of indexing. For indexing, we have to use the [] brackets. Each indexing starts with position one. 0, 1 TRUE and FALSE can also be used for indexing. The indexing vector can be logical, integer or character.

Example:

Output:

Vector Operations

1. Vector Arithmetic

We can perform addition, subtraction, multiplication, and division operation with the two vectors of the same length. the result will be a vector.

Example:

Output:

2. Combining Vectors

Example:

Output:

3. Sorting a Vector

We can sort the elements of a vector through the sort() function.

Output:

4. Deleting a Vector

A vector can be deleted by simply assigning a NULL to it.

Output:

Reference: