# A Gentle Introduction to Vectors in R

R is a language for programming with data. Unlike many other languages, the primitive data types in R are not scalars but vectors. Therefore, understanding how to deal with vectors is crucial to programming or reading the R code. In this post, you will learn about various vector operations in R. Specifically, you will know:

• What are the fundamental data objects in R
• How to work with vectors in R

Let’s get started. A Gentle Introduction to Vectors in R
Photo by Frame Harirak. Some rights reserved.

## Overview

This post is divided into three parts; they are:

• Fundamental Data Objects
• Operations on Vectors
• From Vector to Matrix

## Fundamental Data Objects

In other programming languages like Python, you have fundamental data elements such as integers, floats, and strings. In R, however, the fundamentals are vectors, lists, factors, data frames, and environments. There are data types in R, such as character, numeric, integer, logical, and complex. But R natively deals with vectors of integer, for example, rather than a single integer.

Let’s start with the simplest data object. To create a vector of integers of 5 to 10, you can type 5:10:

The syntax uses a colon to separate the two end values, and R will fill in the rest as consecutive integers. You can assign this vector to a variable and retrieve one of its values:

In R, vectors are indexed from 1, not 0. This follows the convention in the mathematics literature. Indeed you can use multiple indices in a vector to produce a sub-vector, e.g.,

will produce a vector (5,7,9).

The colon syntax works for integer vectors. But a more complicated pattern requires the use of the seq() function, for example:

This creates a vector of numeric with an even step size. To check the data type of the vectors, we can run the following:

Indeed, vectors built by the seq() function are numeric but not integer types because the step size can be arbitrary. To check if a vector is an integer vector, we use is.integer() function. The name “is.integer” has a dot in it. Identifiers with a dot as a legitimate character are a feature of the R syntax.

In the above, we created two vectors. We can concatenate them using:

Note that the integers in vector x have been converted into floating point values to make a consistent vector type. You can convert data explicitly, but there may be side effects. For example, converting the above into an integer vector means rounding off the floating points:

## Operations on Vectors

In R, most operations on vectors are applied elementwise. For example,

In the above, c(10, 9, 8, 7) is to concatenate four 1-element vectors. The operator “%/%” is to do integer division (with the remainder discarded), while the operator “%%” is to return the remainder. Other operators in R are similar to other languages, such as +, -, *, /, ^, for addition, subtraction, multiplication, division, and exponentiation respectively.

The other mathematical operations are as you would expect. For example, this is for exponential function and log:

You may refer to the R documentation for the list of built-in functions. Or you can check out the help using the R command: library(help = "base")

## From Vector to Matrix

A matrix in R is built from a vector. For example, the matrix

$$A = \begin{bmatrix} 9 & 2 & 1 \\ 5 & -1 & 6 \\ 4 & 0 & -2 \end{bmatrix}$$

can be built by filling a vector into three columns:

Note that a vector is filled into a matrix by columns, but you can provide an additional argument, “byrow=TRUE”, to change this behavior.

You can tell the matrix’s dimension with

This output is indeed a vector. Hence you can find the number of rows with:

In this example, you have a square matrix. Hence you can find its determinant and inverse with the following:

As you may have guessed, there are many more matrix operations built-in, including chol(), qr(), and svd() for various matrix decomposition. You will know the inverse above is right by multiplying with the original matrix:

Matrix multiplication uses the operator “%*%” since “*” is for elementwise multiplication. Except for the rounding error in the floating point, the product, as shown above, is an identity matrix.

With a matrix, you can extract a row, a column, or a particular element with the following:

On the contrary, you can build a larger matrix by “binding” two matrices along the rows or along the columns:

### Books

As exercises, you can try:

• Create a vector of “apple”, “banana”, “coconut”, “durian”, then create another vector out of the first with only the preceding three elements
• Find the root mean squared value of -2, 2, 5, 8
• Using the solve() function to find
$$\begin{bmatrix}1 & 1 & 1\\ 0 & 2 & 5\\ 2 & 5 & -1\end{bmatrix}^{-1} \begin{bmatrix}6 \\ -4\\ 27\end{bmatrix}$$

## Summary

In this post, you learned how to manipulate vectors, the fundamental data object in R. Specifically, you learned how to:

• Create a vector in R
• Perform vector operations in R
• Converting a vector into a matrix and performing some matrix operations