# Load packages
library(tidyquant) 
library(tidyverse) 

Q1 dividends Import dividends of Walmart and Target since 2010.

mult_stocks <- tq_get(c("WMT", "TGT"),
                      get  = "dividends",
                      from = "2010-01-01")
mult_stocks
## # A tibble: 88 x 3
##    symbol date       value
##    <chr>  <date>     <dbl>
##  1 WMT    2010-03-10 0.303
##  2 WMT    2010-05-12 0.303
##  3 WMT    2010-08-11 0.303
##  4 WMT    2010-12-08 0.303
##  5 WMT    2011-03-09 0.365
##  6 WMT    2011-05-11 0.365
##  7 WMT    2011-08-10 0.365
##  8 WMT    2011-12-07 0.365
##  9 WMT    2012-03-08 0.398
## 10 WMT    2012-05-09 0.398
## # ... with 78 more rows

Q2 economic data Import the U.S. Unemployment Rate (Percent, Monthly, Seasonally Adjusted) since 2010.

Hint: Find the symbol in FRED.

econdata <- tq_get(c("UNRATE"),
                      get  = "economic.data",
                      from = "2010-01-01")
econdata
## # A tibble: 130 x 3
##    symbol date       price
##    <chr>  <date>     <dbl>
##  1 UNRATE 2010-01-01   9.8
##  2 UNRATE 2010-02-01   9.8
##  3 UNRATE 2010-03-01   9.9
##  4 UNRATE 2010-04-01   9.9
##  5 UNRATE 2010-05-01   9.6
##  6 UNRATE 2010-06-01   9.4
##  7 UNRATE 2010-07-01   9.4
##  8 UNRATE 2010-08-01   9.5
##  9 UNRATE 2010-09-01   9.5
## 10 UNRATE 2010-10-01   9.4
## # ... with 120 more rows

Q3 stock prices Import stock prices of Facebook, Apple, Netflix and Google since 2019.

stock_prices <- tq_get(c("FB", "AAPL", "NFLX", "GOOG"),
                      get  = "stock.prices",
                      from = "2019-01-01")
stock_prices
## # A tibble: 1,908 x 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 FB     2019-01-02  129.  138.  129.  136. 28146200     136.
##  2 FB     2019-01-03  135.  137.  131.  132. 22717900     132.
##  3 FB     2019-01-04  134.  138   134.  138. 29002100     138.
##  4 FB     2019-01-07  138.  139.  136.  138. 20089300     138.
##  5 FB     2019-01-08  140.  143.  140.  143. 26263800     143.
##  6 FB     2019-01-09  143.  145.  141.  144. 22205900     144.
##  7 FB     2019-01-10  143.  145.  141.  144. 16125000     144.
##  8 FB     2019-01-11  143.  145.  143.  144. 12908000     144.
##  9 FB     2019-01-14  142   147.  141.  145. 20520300     145.
## 10 FB     2019-01-15  146.  151.  146.  149. 24069000     149.
## # ... with 1,898 more rows

Q4 stock index Get all stocks in the S&P600 Index using tq_index().

Hint: Google tidyquant r package to find its manual and look for tq_index function on the manual. Alternatively, Google tq_index on the Web to find its documentation.

SP <- tq_index("SP600")
SP
## # A tibble: 601 x 8
##    symbol company    identifier sedol  weight sector  shares_held local_currency
##    <chr>  <chr>      <chr>      <chr>   <dbl> <chr>         <dbl> <chr>         
##  1 BRKS   Brooks Au~ 11434010   2145~ 0.00637 Inform~      110008 USD           
##  2 CPRI   Capri Hol~ G1890L10   BJ1N~ 0.00630 Consum~      224119 USD           
##  3 KNSL   Kinsale C~ 49714P10   BD1M~ 0.00622 Financ~       31695 USD           
##  4 NEO    NeoGenomi~ 64049M20   2598~ 0.00594 Health~      164738 USD           
##  5 YETI   YETI Hold~ 98585X10   BGR7~ 0.00567 Consum~      111567 USD           
##  6 SAIA   Saia Inc.  78709Y10   2982~ 0.00559 Indust~       39044 USD           
##  7 OMCL   Omnicell ~ 68213N10   2789~ 0.00555 Health~       63791 USD           
##  8 EXPO   Exponent ~ 30214U10   2330~ 0.00536 Indust~       76950 USD           
##  9 POWI   Power Int~ 73927610   2133~ 0.00518 Inform~       89183 USD           
## 10 CROX   Crocs Inc. 22704610   B0T7~ 0.00494 Consum~      100643 USD           
## # ... with 591 more rows

Q5 mutate Convert weight in decimals to percentages in S&P600.

Hint: Multiply weight by 100 and save it under the same name, weight. You may Google “dplyr::mutate” to find its documentation.

SP <- 
  SP %>%
  mutate(weight = weight * 100)
SP
## # A tibble: 601 x 8
##    symbol company    identifier sedol weight sector   shares_held local_currency
##    <chr>  <chr>      <chr>      <chr>  <dbl> <chr>          <dbl> <chr>         
##  1 BRKS   Brooks Au~ 11434010   2145~  0.637 Informa~      110008 USD           
##  2 CPRI   Capri Hol~ G1890L10   BJ1N~  0.630 Consume~      224119 USD           
##  3 KNSL   Kinsale C~ 49714P10   BD1M~  0.622 Financi~       31695 USD           
##  4 NEO    NeoGenomi~ 64049M20   2598~  0.594 Health ~      164738 USD           
##  5 YETI   YETI Hold~ 98585X10   BGR7~  0.567 Consume~      111567 USD           
##  6 SAIA   Saia Inc.  78709Y10   2982~  0.559 Industr~       39044 USD           
##  7 OMCL   Omnicell ~ 68213N10   2789~  0.555 Health ~       63791 USD           
##  8 EXPO   Exponent ~ 30214U10   2330~  0.536 Industr~       76950 USD           
##  9 POWI   Power Int~ 73927610   2133~  0.518 Informa~       89183 USD           
## 10 CROX   Crocs Inc. 22704610   B0T7~  0.494 Consume~      100643 USD           
## # ... with 591 more rows

Q6 summarize You want to know what the mean weight is. Calculate the mean weight of S&P600.

Hint: You may Google “dplyr::summarize” to find its documentation.

SP %>%
  summarize(sum = sum(weight))
## # A tibble: 1 x 1
##     sum
##   <dbl>
## 1   100

Q7 group_by and summarize Calculate the sum of weights by sector. Which sector has the greatest weight on S&P600 in aggregate?

Hint: You may Google “dplyr::group_by” to find its documentation.

SP %>%
  group_by(sector) %>%
  summarize(sum = sum(weight))
## # A tibble: 11 x 2
##    sector                   sum
##    <chr>                  <dbl>
##  1 Communication Services  2.43
##  2 Consumer Discretionary 15.1 
##  3 Consumer Staples        3.50
##  4 Energy                  3.03
##  5 Financials             17.4 
##  6 Health Care            11.7 
##  7 Industrials            17.5 
##  8 Information Technology 14.0 
##  9 Materials               5.53
## 10 Real Estate             8.15
## 11 Utilities               1.66

Q8 Hide the messages and warnings, but display the code and its results on the webpage.

Hint: Refer to the RMarkdown Reference Guide.

Q9 Display the title and your name correctly at the top of the webpage.

Q10 Use the correct slug.