DAY 4: OPERATIONS ON R OBJECTS — R programming basics

SaiGayatri Vadali
3 min readDec 22, 2017

--

This article is fourth one in the series “Getting started with data science in 30 days using R programming”. In the previous articles we have known about R objects, their creation and manipulation . Today, we are going to learn about various operations which can be performed on them.

Arithmetic operations

Arithmetic operations in R include addition, subtraction, multiplication and division. As these are binary operations, they need two operands. They are simple and similar to operations in any other programming language.

Following illustration tells about arithmetic operation between two variables in R.

> a <-3
> b <-5
# Addition
> a+b
[1] 8
# Subtraction
> a-b
[1] -2
# multiplication
> a*b
[1] 15
#Division
> a/b
[1] 0.6

Here I would like to tell you about assignment operator “ <-” It assigns the value given to the left of it to the identifier present on it’s right side. You can use ‘=’ too in it’s place. Now that we have got a general idea of performing operations on variables, let us see how they can be done on vectors and matrices.

Operations on Vectors

In order to find if a given object is a vector or not, we have ‘is.vector()’ function which returns a boolean value. Similarly, there is ‘is.matrix()’ function to find if an object is matrix or not.

> a <- 1:4
> is.vector(a)
[1] TRUE

It returns false if the object is not a vector.

Arithmetics with vectors:

Let us take two vector objects of same length and perform all the operations.

#Addition
> a <- 1:6
> b <- 2:7
> a+b
[1] 3 5 7 9 11 13
# Subtraction
> a-b
[1] -1 -1 -1 -1 -1 -1
#multiplication
> a*b
[1] 2 6 12 20 30 42
#Division
> a/b
[1] 0.5000000 0.6666667 0.7500000 0.8000000
[5] 0.8333333 0.8571429

Pretty straightforward and simple, right ? Now let us take vectors of different lengths.

> b <- 1:8
> a <- 1:4
> a+b
[1] 2 4 6 8 6 8 10 12

We can see that a vector of length same as that of ‘b’ formed. This happened because vector ‘a’ which is the smaller one in terms of length repeated itself cyclically till it’s length became equal to that of vector ‘b’.

Now, think what happens if you add a scalar to a vector. Yes, you are right! It gets added to all the elements of vector.

Accessing elements with negative indices

Apart from these, you can even access only required elements with ‘-’ operator. If you want to remove an element of a vector , all you need to do is to make it a negative indexed value. It is shown below

> a <- 1:4
> a[-3]
[1] 1 2 4

Operations on Matrices

Let us take two matrices a and b and perform addition and multiplication operations.

> a <- matrix(1:4,2,2)
> a
[,1] [,2]
[1,] 1 3
[2,] 2 4
> b <- matrix(2:5,2,2)
> b
[,1] [,2]
[1,] 2 4
[2,] 3 5

Addition of matrices

Just type a+b just like variable addition to see the result in the console.

> a+b
[,1] [,2]
[1,] 3 7
[2,] 5 9

Corresponding elements got added.

Multiplication of matrices

> a*b
[,1] [,2]
[1,] 2 12
[2,] 6 20

We can see that corresponding elements are multiplied unlike conventional column-row multiplication of a matrix. Let’s see how to get conventional matrix multiplication.

%*% operator :

We use %*% operator to get that operation.
> a%*%b
[,1] [,2]
[1,] 11 19
[2,] 16 28

t():

Now, let’s get transpose of the matrix. We use t() function to get the transpose.

> a <- matrix(23:26,2,2)
> a
[,1] [,2]
[1,] 23 25
[2,] 24 26
> at <- t(a)
> at
[,1] [,2]
[1,] 23 24
[2,] 25 26

Unit matrix

Unit matrix is that which has all 1s. We can create it as follows

> mat <- matrix(1,2,3)
> mat
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 1

Diagonal of a matrix

> v <- matrix(1:9,3,3)
> diag(v)
[1] 1 5 9
> v
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

With this article, we are done with the nuts and bolts of R. In the next article, we will be getting into functional aspects and statistical details. Stay tuned!!

How did you feel about the article today? Did you feel something could be appreciated? Did you feel something could be added? Please let me know in the comment section.

--

--

SaiGayatri Vadali

An inquisitive Machine Learning Engineer, yoga trainer, fitness freak and a passionate writer!