Comparison Operators & Vectors - R Basics

In this post, we will learn about comparison operators, vectors operations, indexing and slicing.

Hey everyone! Sorry for the inconsistent posting, I recently just picked up a part-time accounting contract job and had work over the weekend so kinda've been juggling my time around. But anyways, I'm back for now and today we are going to be talking about Comparison Operators & Vectors (arguably the most important part of R you will use the most). Let's get started!

What you will learn after following along this post:

  • How to use comparison opearators
  • What is indexing and slicing?
  • What are the difference possibilities and combinations for indexing?

Comparison Operators

Let's start with something easy today to get our learning warmed up. There's nothing much to explain about these so I'll just list them out for you.

Greater Than ">"

> 4 > 10
[1] FALSE

Greater Than or Equal To ">="

> 10 >= 5
[1] TRUE

Less Than "<"

> 5 < 10
[1] TRUE

Less Than or Equal To "<="

> 6 <= 7
[1] TRUE

Equal To "=="

> 345 == 345
[1] TRUE

Not Equal To "!="

> 45 != 45
[1] FALSE

That's pretty much it! Now onto a new concept for beginners, VECTORS. I cannot stress how important vectors are in your upcoming R journey, it's definitely something you will use a lot along the way. So stay focused and read on!

Vectors

Now to create a vector, we use "c(element1,element2,element3......)". Vectors can also be assigned to variables and called out to be used later. For example...

> v1 <- c(1,2,3,4,5)
> v1
[1] 1 2 3 4 5

A vector is very versatile and store any type of data in it like Booleans and characters. However, every vector can only have ONE type of data in it. Here are some example vectors and we can check the class of the vectors using "class()".

> v1 <- c(1,2,3,4,5)
> class(v1)
[1] "numeric"

> v2 <- c('Apple','Orange','Pear')
> class(v2)
[1] "character"

> v3 <- c(TRUE, TRUE, FALSE)
> class(v3)
[1] "logical"

> v4 <- c('Apple',256,372)
> class(v4)
[1] "character"

> v5 <- c(123,456,TRUE,TRUE)
> class(v5)
[1] "numeric"

As you can see from the last 2 examples, I included more than one type of data in my vector, however R will read all the elements in my vector as the same data type based on whichever can cater to all of them. From what I have observed, this is the order on which R processes vectors data types.

Boolean > Numeric > Character

Let's look as v4 as an example, since "Apple" can't be a Boolean or numeric value, then whole vector would be under the character data type. Here are some example for you to guess what data type will appear and you can verify your answers by using class() in RStudio.

c('Big', 'Small', 4567, 9090)

c(TRUE, F, "Tall", "Short")

c(123, TRUE, 124, FALSE)

c("Red", TRUE, 375)

Vector Operations

Now that we learnt about basic arithmetic, comparison operators and vectors, it's time to put them all together. For vectors, operations performed on them will be on a element by element basis. Being from a non-tech background, sounds confusing to me... so let's see some examples.

> v1 <- c(1,2,3,4,5)
> v1 > 3
[1] FALSE FALSE FALSE  TRUE  TRUE
> v1 == 2
[1] FALSE  TRUE FALSE FALSE FALSE

From here, you can see that if you use a comparison operator on a vector, it will automatically loop through the elements in that vector and return a vector of results which you can assign to a variable as well. Here's another example using arithmetic.

> v1 <- c(1,2,3,4,5)
> v2 <- c(1,2,3,4,5)
> v1 + 5
[1]  6  7  8  9 10
> v1 + v2
[1]  2  4  6  8 10

As you can see, when performing addition using 2 vectors, R will take the 1st element of the 1st vector and add that to the 1st element of the 2nd vector and continue doing so for the rest. Now you may be wondering... what happens if both of my vectors don't have the same number of elements in them? Well... let's find out, shall we?

> v1 <- c(1,2,3,4,5)
> v2 <- c(1,2,3)
> v1 + v2
[1] 2 4 6 5 7
Warning message:
In v1 + v2 :
  longer object length is not a multiple of shorter object length

When one of the vectors does not have the same length as the other, R will simply loop the shorter vector until all elements in the longer vector have been completed. For this case, for v1's 4th & 5th element, it simply added v1(4,5) to v2(1,2) resulting in 5 & 7.

Other than addition, there is also subtraction, multiplication, division and tons of other functions that can be used on vectors as well, such as Maximum "max(v1)", Minimum "min(v1)", Product "prod(v1)", Standard Deviation "sd()" and Variance "var()".

Lastly, a very useful function that I like is naming the elements in your vector by using the function names(v1). By default, after creating your vector, the elements just appear without any headers or names. So by using names(), you can essentially put labels/headers for your vector. Maybe an example might make it easier to understand.

#Creating a vector
> v1 <- c(1,2,3,4,5)
> v1
[1] 1 2 3 4 5

#Assigning names for the vector
> names(v1) <- c('A', 'B', 'C', 'D', 'E')
> v1
A B C D E 
1 2 3 4 5 

#You can even use the labels/header to call out certain rows or columns of data
#This is called slicing and we will be covering it right after this (foreshadowing)
> v1['A']
A 
1

Vector Indexing and Slicing

In this section, we will learn about how to call out certain elements within a vector. To call out an element by indexing, use a square bracket [] along with the index number. Take note that in R, the index number starts from 1 (some other languages like Python start from 0).

> v1 <- c('a', 'b', 'c')
> v1[2]
[1] "b"

To call out multiple elements, use a numeric vector within a square bracket. Notice that we can call out elements in any order that we like and we can have duplicates as well.

> v1 <- c('a', 'b', 'c', 'd', 'e', 'f', 'g')
> v1[c(1,3,5,7)]
[1] "a" "c" "e" "g"

> v1[c(3,5,1,7)]
[1] "c" "e" "a" "g"

> v1[c(3,3,5,5)]
[1] "c" "c" "e" "e"

Staying on the topic of indexing, I already showed you that we can call out an element using its assigned name. Now you can combine your knowledge of naming elements within a vector and calling out multiple elements using a vector.

> v1 <- c(1:7)
> v2 <- c('a', 'b', 'c', 'd', 'e', 'f', 'g')
#Assigning v2 elements as names of v1 elements
> names(v1) <- v2
> v1
a b c d e f g 
1 2 3 4 5 6 7 

#Calling out multiple elements using their names
> v1[c('b','d','g','g','a')]
b d g g a
2 4 7 7 1
#You can perform arithmetic operations on the extracted elements as well. 
#Feel free to explore the possibilities and limitations.
> sum(v1[c('b','d','g','g','a')])
[1] 21

I know this has been a long post but we are nearly done! Just two more things to cover. Now it's time for you to combine what you learnt from comparison operators and indexing. I think you probably already know what's the syntax for it, if you do, then great! It means that you fully understood the concepts I talked about previously. But for the ones that are still unsure, here is an example.

> v1 <- c(1:7)
> v1[v1>2]
c d e f g 
3 4 5 6 7 

#You can also assign comparison to variables and use those as a filter in your indexing
> compare <- v1>2
> v1[compare]
c d e f g 
3 4 5 6 7

Congratulations on making it this far! Don't worry, just one last part to go and that is Slicing.

Slicing essentially means extracting a slice or section of a vector / dataframe and to do this, we use the colon ":" symbol. I actually used it twice in the exercise before this. If you noticed it, then you probably already guessed what it does.

> v1 <- c(1:7)
> v1
[1] 1 2 3 4 5 6 7
> v2 <- c(10:25)
[1] 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

If you are wondering whether you can use slicing within indexing, then you are on the right path!

> v2 <- c(10:25)
> v2[4:8]
[1] 13 14 15 16 17

Okay that's all for today. Great job on reaching the end of this post! You should be proud of yourself. Programming is about curiosity and fidgeting with the resources you have. At the beginner phase, always do your best to find things to play or test. Through the writing of this post, I have learned so much and I hope you learnt from this as well. Thank you all so much and see you guys next time!