Using the given code, answer the questions below.

library(tidyquant) 
library(tidyverse) 

stocks <- tq_get("AAPL", get = "stock.prices", from = "2016-01-01")
stocks
## # A tibble: 782 x 7
##    date        open  high   low close   volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 2016-01-04 103.  105.  102   105.  67649400     99.5
##  2 2016-01-05 106.  106.  102.  103.  55791000     97.0
##  3 2016-01-06 101.  102.   99.9 101.  68457400     95.1
##  4 2016-01-07  98.7 100.   96.4  96.4 81094400     91.1
##  5 2016-01-08  98.6  99.1  96.8  97.0 70798000     91.6
##  6 2016-01-11  99.0  99.1  97.3  98.5 49739400     93.1
##  7 2016-01-12 101.  101.   98.8 100.0 49154200     94.4
##  8 2016-01-13 100.  101.   97.3  97.4 62439600     92.0
##  9 2016-01-14  98.0 100.   95.7  99.5 63170100     94.0
## 10 2016-01-15  96.2  97.7  95.4  97.1 79010000     91.7
## # ... with 772 more rows

stocks %>%
  ggplot(aes(x = date, y = close)) +
  geom_line()

Q1. How many columns (variables) are there?

There are 7 variables.

Q2. What are the variables?

The variables are: date, open, high, low, close, volume, and adjusted.

Q3. Add Microsoft stock prices, in addition to Apple.

stocks_2 <- tq_get(c("AAPL", "MSFT"), get = "stock.prices", from = "2016-01-01")
stocks
## # A tibble: 782 x 7
##    date        open  high   low close   volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 2016-01-04 103.  105.  102   105.  67649400     99.5
##  2 2016-01-05 106.  106.  102.  103.  55791000     97.0
##  3 2016-01-06 101.  102.   99.9 101.  68457400     95.1
##  4 2016-01-07  98.7 100.   96.4  96.4 81094400     91.1
##  5 2016-01-08  98.6  99.1  96.8  97.0 70798000     91.6
##  6 2016-01-11  99.0  99.1  97.3  98.5 49739400     93.1
##  7 2016-01-12 101.  101.   98.8 100.0 49154200     94.4
##  8 2016-01-13 100.  101.   97.3  97.4 62439600     92.0
##  9 2016-01-14  98.0 100.   95.7  99.5 63170100     94.0
## 10 2016-01-15  96.2  97.7  95.4  97.1 79010000     91.7
## # ... with 772 more rows
stocks_2
## # A tibble: 1,564 x 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 AAPL   2016-01-04 103.  105.  102   105.  67649400     99.5
##  2 AAPL   2016-01-05 106.  106.  102.  103.  55791000     97.0
##  3 AAPL   2016-01-06 101.  102.   99.9 101.  68457400     95.1
##  4 AAPL   2016-01-07  98.7 100.   96.4  96.4 81094400     91.1
##  5 AAPL   2016-01-08  98.6  99.1  96.8  97.0 70798000     91.6
##  6 AAPL   2016-01-11  99.0  99.1  97.3  98.5 49739400     93.1
##  7 AAPL   2016-01-12 101.  101.   98.8 100.0 49154200     94.4
##  8 AAPL   2016-01-13 100.  101.   97.3  97.4 62439600     92.0
##  9 AAPL   2016-01-14  98.0 100.   95.7  99.5 63170100     94.0
## 10 AAPL   2016-01-15  96.2  97.7  95.4  97.1 79010000     91.7
## # ... with 1,554 more rows

Q4. How many variables are there now? Any new variable?

There are 8 variables. The new variable is Symbol.

Q5. On how many days either of the two stocks closed higher than $200 per share?

stocks_2 %>% filter(close > 200)
## # A tibble: 72 x 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 AAPL   2018-08-01  199.  202.  197.  202. 67935700     199.
##  2 AAPL   2018-08-02  201.  208.  200.  207. 62404000     205.
##  3 AAPL   2018-08-03  207.  209.  205.  208. 33447400     206.
##  4 AAPL   2018-08-06  208   209.  207.  209. 25425400     207.
##  5 AAPL   2018-08-07  209.  210.  207.  207. 25587400     205.
##  6 AAPL   2018-08-08  206.  208.  205.  207. 22525500     205.
##  7 AAPL   2018-08-09  207.  210.  207.  209. 23469200     207.
##  8 AAPL   2018-08-10  207.  209.  207.  208. 24611200     206.
##  9 AAPL   2018-08-13  208.  211.  208.  209. 25869100     207.
## 10 AAPL   2018-08-14  210.  211.  208.  210. 20748000     208.
## # ... with 62 more rows

There are 72 days where either stock rose above $200.

Q6. On how many days Apple stock closed higher than $200 per share?

stocks_2 %>% filter (close > 200, symbol == "AAPL")
## # A tibble: 72 x 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 AAPL   2018-08-01  199.  202.  197.  202. 67935700     199.
##  2 AAPL   2018-08-02  201.  208.  200.  207. 62404000     205.
##  3 AAPL   2018-08-03  207.  209.  205.  208. 33447400     206.
##  4 AAPL   2018-08-06  208   209.  207.  209. 25425400     207.
##  5 AAPL   2018-08-07  209.  210.  207.  207. 25587400     205.
##  6 AAPL   2018-08-08  206.  208.  205.  207. 22525500     205.
##  7 AAPL   2018-08-09  207.  210.  207.  209. 23469200     207.
##  8 AAPL   2018-08-10  207.  209.  207.  208. 24611200     206.
##  9 AAPL   2018-08-13  208.  211.  208.  209. 25869100     207.
## 10 AAPL   2018-08-14  210.  211.  208.  210. 20748000     208.
## # ... with 62 more rows

There are 72 days where Apple closed above $200. Microsoft never rose above $200 per share.

Q7. Create a new variable, MC, market capitalization.

Hint: Use dplyr::mutate. Market cap is given by the formula, MC = N × P, where MC is the market capitalization, N is the number of shares outstanding, and P is the closing price per share.

stocks <- stocks_2 %>% mutate(mc = volume * close)
stocks
## # A tibble: 1,564 x 9
##    symbol date        open  high   low close   volume adjusted          mc
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>       <dbl>
##  1 AAPL   2016-01-04 103.  105.  102   105.  67649400     99.5 7126864155.
##  2 AAPL   2016-01-05 106.  106.  102.  103.  55791000     97.0 5730293554.
##  3 AAPL   2016-01-06 101.  102.   99.9 101.  68457400     95.1 6893659975.
##  4 AAPL   2016-01-07  98.7 100.   96.4  96.4 81094400     91.1 7821554637.
##  5 AAPL   2016-01-08  98.6  99.1  96.8  97.0 70798000     91.6 6864574009.
##  6 AAPL   2016-01-11  99.0  99.1  97.3  98.5 49739400     93.1 4900823032.
##  7 AAPL   2016-01-12 101.  101.   98.8 100.0 49154200     94.4 4913453783.
##  8 AAPL   2016-01-13 100.  101.   97.3  97.4 62439600     92.0 6080992582.
##  9 AAPL   2016-01-14  98.0 100.   95.7  99.5 63170100     94.0 6286688162.
## 10 AAPL   2016-01-15  96.2  97.7  95.4  97.1 79010000     91.7 7674241063.
## # ... with 1,554 more rows

Q8. Keep only three variables symbol, date, close and market capitalization and drop the other variables.

stocks %>% select(symbol, date, close, mc)
## # A tibble: 1,564 x 4
##    symbol date       close          mc
##    <chr>  <date>     <dbl>       <dbl>
##  1 AAPL   2016-01-04 105.  7126864155.
##  2 AAPL   2016-01-05 103.  5730293554.
##  3 AAPL   2016-01-06 101.  6893659975.
##  4 AAPL   2016-01-07  96.4 7821554637.
##  5 AAPL   2016-01-08  97.0 6864574009.
##  6 AAPL   2016-01-11  98.5 4900823032.
##  7 AAPL   2016-01-12 100.0 4913453783.
##  8 AAPL   2016-01-13  97.4 6080992582.
##  9 AAPL   2016-01-14  99.5 6286688162.
## 10 AAPL   2016-01-15  97.1 7674241063.
## # ... with 1,554 more rows

Q9 Plot daily closing stock prices for both stocks by mapping symbol to color.

Hint: Use ggplot2::ggplot.

stocks %>%
  ggplot(aes(x = date, y = close, colour = symbol)) +
  geom_line()