Vector Arithmetic in R: Made Easy 2024

The post is about vector arithmetic in R Language. In R, different mathematical operations can be performed on vectors, that is vectors can be used in arithmetic expressions. The vector arithmetic operations are performed element by element.

It is important to note that vectors occurring in the same mathematical expression need not be of the same length (size). The shorter vectors in the arithmetic expression are recycled until they match the length of the longest vector.

Vector Arithmetic Operations

The vector arithmetic operations can be performed using arithmetic operators and vector functions. The +, -, *, /, and ^ are elementary arithmetic operators. The arithmetic functions are also available, such as, log, exp, sin, cos, tan, sqrt, and so on. The max() and min() functions returns the largest and smallest elements of a vector, respectively. Similarly, the range() function results in a vector of length two having minimum and maximum values from the vector, that is, c(min(x), max(x)).

The length(x) function returns the number of elements (size or number of observations) in a vector say $x$, sum(x) gives the total (sum) of the elements in vector $x$, and prod(x) returns the product of elements.

Instead of performing simple arithmetics (+, -, *, and /), we will use some functions for arithmetic that can be performed on a vector.

Vector Arithmetic in R: Examples

The basic vector arithmetic in R can be performed just like adding numbers on a calculator.

x <- c(1, 2, 3, 4, 5)
y <- c(4, 5, 6, 7, 8)

# Addition
x + y

# Subtraction
x - y

# Multiplication
x * y

# Division
x / y

# Exponentiation
x ^ y

One can compute the average (mean value) of a vector by performing arithmetics on a vector, such as

x <- c(5, 10, 5, 3, 5, 6, 7, 8, 4, 3, 10)
sum(x)/ length(x)

## Output
6

The built-in function for the computation of the average value of a vector is mean(), that is mean(x).

mean(x)

## output
6

The variance can also be computed by performing arithmetics on a vector say $x$.

sum((x - mean(x))^2)/ (length(x)-1)

## Output
6.2
Vector Arithmetic in R Language

The built-in function for sample variance is var(x). Note that if the argument var() is a $n$-by-$p$ matrix, a $p$-by-$p$ matrix of the sample covariance matrix will return.

var(x)

## Output
6.2

The sort(x) function returns a vector of the same size as $x$ with the elements arranged in increasing order.

sort(x)

## Output
[1]  3  3  4  5  5  5  6  7  8 10 10

The min() and max() functions are used to select the smallest and largest values from the argument, even if the argument contains several vectors.

In summary, Vector arithmetic is a fundamental aspect of R programming, enabling efficient and concise mathematical operations on sequences of elements. By understanding the basic operations, vector recycling, and available functions, you can effectively leverage vectors to solve a wide range of problems in data analysis and scientific computing.

https://rfaqs.com vector arithme5ics

https://itfeature.com, https://gmstat.com

Leave a Reply

Discover more from R Language Frequently Asked Questions

Subscribe now to keep reading and get access to the full archive.

Continue reading