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 & bis true only if bothaandbare true.|(Or): A union.a | bis true if eitheraorbis 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.
Cet exercice fait partie du cours
Intermediate R for Finance
Instructions
- When is
ibmbetween171and176? Add the logical vector tostocksasibm_buy_range. - Check if
paneradrops below213.20or rises above216.50, then add it tostocksas the columnpanera_spike. - Suppose you are interested in dates after
2017-01-21but before2017-01-25, exclusive. Useas.Date()and&for this. Add the result tostocksasgood_dates. - Print
stocks.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# IBM buy range
___
# Panera spikes
___
# Date range
___
# Print stocks
___