# Vectors in R With Examples [Latest]

Jun 3, 2022

Vectors in R are the fundamental data types. This is because the R compiler treats all scalars (numerics, integers, etc.) and matrices as special cases of vectors.

From a data scientist’s perspective, you can consider a vector as a collection of observations across an interval of time, such as temperatures read every day, total sales for the day, etc. R provides several relevant functions to handle vectors from this perspective.

## Creating Vectors in R

The creation of a vector is done using the `c()` function.

R language provides us the functionality to dynamically calculate values and assign them to vectors.

We can create vectors using previously created variables.

We can also create a vector using two or more of the existing vectors.

Vectors can have any number of items of the same data type (also sometimes known as the mode). Note: We cannot mix data types when we’re creating vectors in R.

## Operations on Vectors in R Language

Vectors are stored contiguously in the memory, similar to C. You can index the elements in a vector, extract subsets of vectors, sort and perform routine mathematical operations over vectors element-wise. We shall look at some examples that make these clear.

### Indexing elements of a vector

The elements of a vector can be extracted by using their index in a manner similar to accessing array elements. The following code snippet provides you with an example.

When you try accessing an element beyond the vector’s size, R returns an NA value.

### Getting the length of a vector in R language

Oftentimes, we deal with data from a dataset we download off the internet. We read entire columns into vector variables and may not be aware of the dimensions beforehand. In these cases, the length will be an important parameter to know so that we don’t run into NA values when working with data. The length of a vector can be known using a `length()` function.

### Subsetting vectors in R

When dealing with long vectors, it is sometimes necessary to extract only the elements of interest from the vector. We can do this by making use of subsetting in R.

We will look deeper into subsetting when we are working with real datasets in our further tutorials.

### Generating Sequences

Sequences are vectors in R that are generated using a sequence operator (`:`). They can also be generated using the `seq` function. These two methods are illustrated below.

Instead of using a `by` parameter, you can also supply a `length.out` parameter to indicate the length you need and get evenly spaced values from the starting range to ending range.

Vectors can be repeated using the `rep` function in R. The usage of `rep` is illustrated below.

The x can be replaced by a vector to obtain a repeating vector as follows.

### Sorting vectors

Vectors can be sorted in ascending or descending order using the `sort()` function in the following manner.

### Vector arithmetic

Vector arithmetic has been covered in the operators in R discussion earlier. One important point about vector arithmetic is recycling. When the specified vector operation has two vectors with mismatched length as operands, R simply recycles the values from the shorter vector until it reaches the length.