In this video, I'll cover two important
structures in any language--
the if statement and logical operators.
I'll also introduce this if/else and which functions.
I'm going to occasionally rely on the stock data used the
last video, but to keep things simple, I'm also going to omit
any rows that have an NA value, as these represent
missing observations in the data.
An if statement is used to execute some section of code
on the condition that a particular statement is true.
In this setup, the code inside the braces will run if the
condition is true.
For example, suppose I want to check whether the first entry
in the stock data was up or down.
If it was up, I want to create a variable called status that
takes the value up.
It would also be informative to label the status as down if
the stock is down.
So I'll add an else statement.
The code in the second set of braces will execute if the
condition is false.
However, this still isn't quite right.
What if the stock opens and closes at the same price?
I can add a condition for the second set of code using a
second if statement after the else command.
Then, I'll add another else command at the end that labels
status with a value of flat if the stock is flat for the day.
I also want to briefly mention the if/else function, which
could be used in this context to create an up, down, flat
vector for all the elements in the stocks data in just a few
lines of code.
The if/else function takes three arguments--
a vector of length one or more that contains true and false
values, a value to take for each true instance, and a
value to take for each false instance.
Though this still isn't quite right, because I need to
return a value of down if the close is lower than the open.
I can do this by creating the second condition, and I'd need
one more if/else command.
Here, I've done something subtle.
I have provided the status argument for those cases where
the close is not lower than the open.
I've exploited a special feature of the if/else
function, which is that when a vector of length greater than
one is provided in the second or third arguments, if/else
will be performed,
element-wise, for that argument.
I'll leave it to you to verify that this code
works and why it works.
You should think about each possible case.
A stock can be up, down, or flat, and I strongly recommend
you create some test data and work your way through each
step of the code.
Next, I want to talk about logical operators.
I'm going to create a vector with 10 random integers
between negative 1 and 13, and a second
vector in the same way.
I'd like to know whether all of the elements in x are
greater than zero.
I might intuitively start by using the command x>0.
However, as R does in many other scenarios, it also
performs this check element-wise.
Here you can see I have a vector of
true and false values.
I can use the all function around x>0 to answer the
question as to whether all of the values of x are
greater than zero.
We can also check other things, such as, do any
elements of x line up with those in y?
This, again, returns a vector of true and false, but this
time, I can use the any function to check whether any
of the values are equal.
We can also do other checks that relate x and y.
For example, consider the separate commands x>0 and y>0.
If I wanted to check in which elements this was true in both
vectors, I could use a single
ampersand between the commands.
Here, the single ampersand means to check whether both
the first and the second condition are true on an
element-wise basis.
There's a similar or command using a pipe, or a vertical
line character, that creates an element-wise basis on
whether one statement or the other statement is true.
R, like other languages, also has a double ampersand and
double pipe notation, though I'm going to move on.
If you're interested in this, you should check out the help
file for x or.
The last topic I wanted to cover in this video is the
which function, used to identify which elements or
observations satisfy a particular condition.
For example, I'd like to know which observations had a big
intraday fluctuation.
To begin, I'll create an object called fluctuation that
gives the raw intraday fluctuation.
I can scale the fluctuation by dividing the difference by the
opening share price of the day.
Next, I'll create a condition for whether the fluctuation
was greater than say, 25%.
This is a Boolean vector of true and false values that
report whether each row in the data frame satisfied or did
not satisfy this big fluctuation condition.
If I wanted a vector reporting just those rows that satisfy
the condition, I can use the which function.
These rows represent observations where the stock
price fluctuated by more than 25% in a day.
If I wanted, I could examine each of these rows in the
original data set.
In the next video, I'll talk about for loops.