• ## Summary

Search form
• 00:01

MATT DENNY: Hi, everyone.This is your instructor, Matt Denny,and welcome to this lecture on comparison operators.So what are comparison operators?Well, they're the R syntax, they'rethe R code that lets us check if certain conditions aremet in R. So for example, we mightwant to compare two numbers.Maybe we didn't know the order of the integers.

• 00:23

MATT DENNY [continued]: So we had the integer 4 and then the number5, that these two integers, and we might wantto ask which one was smaller.So we could ask the question, Is 4 less than 5?And we would do that using a comparison operator, the lessthan comparison operator.And what that would do when we enter that into our R consolewas it would return to us, in this case, the value

• 00:45

MATT DENNY [continued]: True, this special Boolean value or truth value in R.And what that will do is that allowsus to encapsulate all sorts of logic in R.It allows us to say are two things the same,are they different, is one bigger than the other,is one smaller than the other.And this can be really critical for givingour programs a brain.

• 01:06

MATT DENNY [continued]: And by that I mean if we want to have a program thatcan intelligently adapt to the kind of datathat we give it, so if some conditions are metit'll do one thing, if other conditions aremet it'll do something else, the way we usuallydo that is through the use of these comparison operators.In conjunction with some other concepts, but at their core,

• 01:26

MATT DENNY [continued]: it's these comparison operators.These pop up all over the place in all sorts of data managementtasks, and in all sorts of programming tasks moregenerally.And I think it's really important to cover these.These will sort of serve--this is our foundational nuts and boltsthat we're going to use throughout the restof this course.Now in addition to these things being very useful,

• 01:46

MATT DENNY [continued]: there are also some common ways that wecan get tripped up using them.And I'm going to cover that as well.So I'm going to zoom over to my desktop,and we're going to open up RStudioand we're going to look at some R code for how we do this.OK, so I'm here on my desktop and I'mgoing to open up my basic R programming script.

• 02:09

MATT DENNY [continued]: So I can double-click that.It's going to open up for me in RStudio.OK, so we've already covered the first portion of this scripton lecture one, the variables comments in math.We're going to scroll down to--whoops-- line, let's see here, 66.So comparison operators.

• 02:30

MATT DENNY [continued]: All right, so that example I gave you earlier, the is 4 lessthan 5, so here we have an example of it.This is the syntax for asking R that question.So essentially, what we see here is we see 5,and then I guess that's the left caret, and then 6.And so what we're saying in words is 5 is less than 6.

• 02:51

MATT DENNY [continued]: So that is our statement and we'regoing to check the truth value of this statement.We're going to make a comparison.So again, I'm going to use my Command plus Enter, or Controlplus Enter if you're on a Windows machine,to enter this statement into the R console,to copy it from over here on my script into my R console.So I'm going to do that right now.Alternatively, we could have highlighted it

• 03:12

MATT DENNY [continued]: and clicked the Run button, or justclicked anywhere on this line and clicked the Run button.And again, what we see here, so 5 is, in fact, less than 6.So this comparison operator will return True.So we can also ask, is 5 greater than 6?Well, we know that that's not true.And as we can see, it returns False.

• 03:35

MATT DENNY [continued]: Now these ones are sort of simple.You probably seen these in a high school mathclass or some math class, this idea of less than or greaterthan.What about if we want to know if two things are equivalent,if they're equal?So the way that we do that, the comparisonoperator that we use in R to do this is the double equal sign.So the way we would read this is 5 is equal equal to 5,

• 03:58

MATT DENNY [continued]: or in other words, 5 is equal to 5.Are these two numbers the same?So let's run our code here, and we'llsee that that returns True.If we had something like 6 equal equals 8--whoops, I don't want to do that in there.Well, we can run this anyway.We would see that that would return False, right?So the equal equal sign is honestly

• 04:21

MATT DENNY [continued]: the one you'll probably use the most because oftentimes,say I had a whole bunch of names that I wantedto look for until I found the name Susan Smith, or JohnSmith, or something like that.What I could do is I could check each one of themagainst the string John Smith, or Susan Smith,and I could wait until I found a comparison operator that

• 04:43

MATT DENNY [continued]: returned True.And that would then tell me, oh, I found this person.Again, this is a way of giving your computer a brain.Just as much as we can see if two things are equal,we can also ask, are they not equal?So I don't care if it's greater than, less than,if there's any difference.Just tell me if they're not equal.And to do that, we use the not equal, the exclamation point

• 05:05

MATT DENNY [continued]: equal comparison operator.So let's enter that in here, and weshould expect this to return True because 5is, in fact, not equal to 6.The last thing that we often want to dois we want to sometimes know if something is less thanor equal to something else.So for example, if we wanted to knowif 5 is less than or equal to, so that's

• 05:25

MATT DENNY [continued]: how we would read this, less than or equal to 5,then we would use this sort of left double arrow pointingconfiguration of characters, the left caret and then equal sign.And that will give us the less thanor equal to result that we want.So these are the five basic comparison operators.

• 05:46

MATT DENNY [continued]: There are a few more that you can use down the line,but we're going to start with these five basic comparisonoperators that really form the basis of a lot of the kindof things we'll want to do when we're doing datamanagement, when we're doing data science,when we're doing any sort of programming in R.OK, so right now what we've been doingis we've been comparing numbers.

• 06:08

MATT DENNY [continued]: Now we can also compare strings.So we can ask the question, is dog equal equal to cat?And we can see that that's false.If we were to now change and instead dodog equals equals dog, we can see that that's true.So what we've been doing is we've been working with sort

• 06:28

MATT DENNY [continued]: of the basic units in R. But what happens if wewant to work with variables?Remember, variables can store values for us.And so they can be a lot more flexible, a lot more helpful.It's a cleaner way to represent information in R.So one of the first things you'llnotice about comparison operatorsis that they try to be helpful.

• 06:50

MATT DENNY [continued]: So, by that, I mean if you give, for example,if you try and compare the number 5,345 to the string5,345.Where it's now a string like dog or cat,we've put it in between the quotation marks,it will, automatically, try and take that string

• 07:11

MATT DENNY [continued]: and convert it into a number, if it finds that there are onlynumbers in the string.So if we run this line of code, we'llsee that it, actually, returns true.Now, this can sometimes be desirable behavior,but sometimes not.And this is an important thing for youto keep track of, so that you don't get mixed up or messed upwhen you're doing comparisons in R. So because here,

• 07:34

MATT DENNY [continued]: maybe the number 5,345 has some different meaning for usthan a string 5,345.If I were to modify this and say 5,345 cats,which is a lot of cats, and I were to hit Enter in here.Now we would see that this returns false.So this is just something to keep an eye out for,

• 07:56

MATT DENNY [continued]: is that R will try--it will do its best to transform numbers into--to transform strings into numbersif the first thing that it's comparing to is also a number.So you just want to watch out for that.To make sure that you don't have thingsbeing treated the same that you didn'texpect to be treated the same.So one question you might reasonably have is,

• 08:20

MATT DENNY [continued]: why the double equals sign?So the standard way that we assign valuesto a variable, which we've gone over in the previous lecture,is using the assignment operator,this little left arrow.So if I want to create a variable,I want to call that variable I, and thenI want to assign into I the value 5,

• 08:42

MATT DENNY [continued]: I would use the standard assignment operator.Now the users of R, they wanted to makeR be similar to some other programminglanguages that have a different assignment operator.And one of the standard assignment operatorsin a lot of programming languagesis just the single equal sign.So I equals 6.So the single equals sign is another way

• 09:04

MATT DENNY [continued]: to assign values to a variable.This is a nice thing for some people who arecoming from other languages.But I would really discourage youfrom using the single equals assignment operator.And I would, definitely, encourageyou to stick to the left arrow assignment operator.

• 09:26

MATT DENNY [continued]: And the reason for this is that it's really easy,when you're skimming of your code,to confuse this I equals 6 with I equals, equals6 or to mess that up in some way.And so, in general, it's a good practice,it's a best coding practice, to always usethe assignment operator.

• 09:47

MATT DENNY [continued]: I'm a big fan of you doing that.And I would suggest you do it off the bat.So, anyhow, let's say we've assigned the value5 to our variable I. So now, as you can see down here,we have this value, and it's called I,and it's stored in this little box where--of our variable called I, we've stored the number 5.

• 10:10

MATT DENNY [continued]: Now what we can do is we can ask--let's use our assignment--let's use our comparison operatorsto compare this variable to some things.So one question we could ask is, is the variable Iequal to the letter I?And that's what we're doing on line 91 here.

• 10:30

MATT DENNY [continued]: So I'm going to hit Enter.And we're going to see that that's false.And the reason that that's false isbecause R is going to take what's inside of I,and it's going to say, oh, it's the number 5.And then, it's going to try and compare itto the letter I. So it may even try and convert I into--the letter I into a number.

• 10:50

MATT DENNY [continued]: But that number is going to be null or NA or something.And that's not going to be the same as the number 5.So even though these two things look the same,we see an I over here on the left, and another I over hereon the right.The issue is that, now, we're comparing a string, somethingthat's literal, something that has the value of the letter I

• 11:11

MATT DENNY [continued]: in it, to a variable that we just call, we refer to as I,but could store whatever we want in it.Remember, I could also be equal to 456 or something like that.And again, this would also not be equal to the letter I.So now what we could do, if we wanted

• 11:32

MATT DENNY [continued]: this to be a valid comparison, if we wanted to, actually,be able to compare the letter I to the variable I,we would need to assign the letterI to the variable I, which is kindof a little bit of a mind trip.

• 11:54

MATT DENNY [continued]: So let's do that.We're going to use the single equals operator here,which again, is bad form.Alternatively, we could use the assignment operator,which is good form.It won't change our result. But now we can see,so the single equals operator-- and thisis where it's easy to get confused-- the single equalsoperator has now changed the value that's

• 12:16

MATT DENNY [continued]: stored by our variable I to be the letter I.And now we're going to use the double equals operator,comparison operator, to compare the value thatis stored in the variable I, which is the letter I,to the letter I.So let's run this now.And we can see that this now returns true

• 12:37

MATT DENNY [continued]: because the thing that is stored inside our variable,is the same thing that we are comparing to.Alternatively, I could have said,I want to set I to be equal to 5.Now, I could say, "Is I equals, equals to 5.And here, this would also return truebecause now what we're doing is we'recomparing the thing that's stored inside of the variable I

• 12:59

MATT DENNY [continued]: to the number 5.So comparison operators, again, we'regoing to use them a lot more.They're really a critical basic component of the R programminglanguage, of any programming language,that allow you to encapsulate logic for your programs.And we're just going to take this forward with us.

• 13:20

MATT DENNY [continued]: And so I will see you in the next video.And thank you very much for watching.

### Video Info

Series Name: Practical Data Management with R

Episode: 11

Publisher: SAGE Publications Ltd

Publication Year: 2017

Video Type:Lecture

### Segment Info

Segment Num.: 1

Persons Discussed:

Events Discussed:

Keywords:

## Abstract

Matt Denny explains comparison operators; what they are, what they do, and how they can be used to work with different types of variables in R.

Introduction to RStudio: Comparison Operators

Matt Denny explains comparison operators; what they are, what they do, and how they can be used to work with different types of variables in R.

Copy and paste the following HTML into your website