All together now!
Great! You have learned a lot about operators and subsetting. This will serve you well in future data analysis projects. Let's do one last exercise that combines a number of operators together.
A new version of the stocks
data frame is available for you to use.
This exercise is part of the course
Intermediate R for Finance
Exercise instructions
- First, print
stocks
. It contains Apple and Microsoft prices for December, 2016. - It seems like you have missing data. Let's investigate further. Use
weekdays()
on thedate
column, and assign it tostocks
as the column,weekday
. - View
stocks
now. The missing data is on weekends! This makes sense, the stock market is not open on weekends. - Remove the missing rows using
subset()
. Use!is.na()
onapple
as your condition. Assign this new data frame tostocks_no_NA
. - Now, you are interested in days where
apple
was above117
, or whenmicr
was above63
. Use relational operators,|
, andsubset()
to accomplish this withstocks_no_NA
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# View stocks
___
# Weekday investigation
stocks$weekday <-
# View stocks again
___
# Remove missing data
stocks_no_NA <- ___
# Apple and Microsoft joint range
___