And / Or
You might want to check multiple relational conditions at once. What if you wanted to know if Apple stock was above 120, but below 121? Simple relational operators are not enough! For multiple conditions, you need the And operator &
, and the Or operator |
.
&
(And): An intersection.a & b
is true only if botha
andb
are true.|
(Or): A union.a | b
is true if eithera
orb
is true.
apple <- c(120.00, 120.08, 119.97, 121.88)
# Both conditions must hold
(apple > 120) & (apple < 121)
[1] FALSE TRUE FALSE FALSE
# Only one condition has to hold
(apple <= 120) | (apple > 121)
[1] TRUE FALSE TRUE TRUE
The stocks
data frame is available for you to use.
Diese Übung ist Teil des Kurses
Intermediate R for Finance
Anleitung zur Übung
- When is
ibm
between171
and176
? Add the logical vector tostocks
asibm_buy_range
. - Check if
panera
drops below213.20
or rises above216.50
, then add it tostocks
as the columnpanera_spike
. - Suppose you are interested in dates after
2017-01-21
but before2017-01-25
, exclusive. Useas.Date()
and&
for this. Add the result tostocks
asgood_dates
. - Print
stocks
.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# IBM buy range
___
# Panera spikes
___
# Date range
___
# Print stocks
___