OK anatomy of the if statement if statement or syntax is relatively simple You've got three components
of the formula your logical test your value if true and your value is false.
Your logical test is basically any test that results in either true or false.
So you could say test that the value of a one equals Google or that the numerical value in B2 is less
than 100.
You can also incorporate more complicated formulas here.
You could say if the max value in column A is between 25 and 30 and so on so forth it's literally an
infinite number of tests.
You could put here next you've got value if true.
This is what you want Excel to return or to do if your test proves true.
So you could just put a number or text string here.
You could also put a formula in which case Excel would evaluate that formula.
If your logical test was true and then as you might expect value of false does the same thing.
It's what you want Excel to do in the case that your logical test is false.
So let's jump into excel and do a hands on demo.
Open the section three logical operators document and we'll work from there.
Just give you a quick background of what we're looking at.
We've got weather data from a station at Boston Logan Airport.
Don't pay too much attention because a I made up some of these numbers but you get the idea.
We've got latitude and longitude date and month.
Oh yes of temperature and precipitation in Fahrenheit and millimeters respectively.
And from that basic information we're going to be building out additional categorical variables like
freeze climate precipitation type conditions et cetera using logical operators in the rest of this section
for this lecture we're going to start with column I the freeze column and all we want to do here is
say let's create a flag or a binary variable that equals the yes if the temperature is freezing.
In other words if it's below 32 degrees Fahrenheit.
Otherwise return no.
So mean you use an IF statement to do this.
We're going to take it nice and slow for the first time.
Basically image can take equals if and open the parenthesis as you can see this little helper box has
popped up.
You can see the three components that we just talked about.
And again the first thing I need is the logical test.
So going arrow over to G-2 which is where my temperature field lives the less than 32.
So that's my test when I get calmer.
Now I need to input the value if true since I want text here.
I need to surround it in quotes and I'm just going to type yes.
Comma again brings me to the value false peace.
Same idea.
No surround surrounded in quotes.
Close that parenthesis so you'll note that I left my reference to G-2 completely relative as opposed
to fixing it.
And that's because when I apply this formula down I want my reference to change from G-2 G-3 G-4 because
I always wanted to refer to the proper temperature in the proper row.
So as you can see double click the lower right corner of the formula cell and it is populated all the
values down as far as there's data which is exactly what I want.
Now just to kind of spot check that it's working properly as you can see we have some cases of missing
data.
Any values.
Don't worry that was intentional.
We'll deal with that momentarily.
But based on the values that it do have 37 degrees is no because it's above freezing 12 and 23 are both
below freezing.
It all looks good.
So there you go.
Basic example of using the if statement