Using the given code, answer the questions below.

library(tidyquant) 
library(tidyverse) 

stocks <- tq_get("AAPL", get = "stock.prices", from = "2016-01-01")
stocks

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

Q1. How many columns (variables) are there?

There are 7 columns (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 <- tq_get(c("AAPL", "MSFT"), get = "stock.prices", from = "2016-01-01")
stocks

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

There are 8 variables and there is another variable called symbol

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

Hint: Use dplyr::filter.

filter(stocks, close > 200)

stocks%>% filter(close > 200)

There are 72 days where the two stocks closed over $200

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

Hint: Use dplyr::filter.

filter(stocks, close > 200)

stocks%>% filter(close > 200,symbol == "AAPL")

There are 72 days where Apple stock closed over $200

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 %>%
  mutate(MC = volume * close)
stocks

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

Hint: Use dplyr::select.

stocks <- stocks %>%
  select(symbol, date, close, MC)
stocks

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, color = symbol)) +
  geom_line()