# Load packages
library(tidyquant) 
library(tidyverse) 

Q1 dividends Import dividends of Walmart and Target since 2010.

tg_divs  <- tq_get("TGT", get = "dividends", from = "2010-01-01")

tg_divs
## # A tibble: 45 x 3
##    symbol date       value
##    <chr>  <date>     <dbl>
##  1 TGT    2010-02-17  0.17
##  2 TGT    2010-05-18  0.17
##  3 TGT    2010-08-18  0.25
##  4 TGT    2010-11-17  0.25
##  5 TGT    2011-02-14  0.25
##  6 TGT    2011-05-16  0.25
##  7 TGT    2011-08-16  0.3 
##  8 TGT    2011-11-14  0.3 
##  9 TGT    2012-02-13  0.3 
## 10 TGT    2012-05-14  0.3 
## # ... with 35 more rows
wm_divs  <- tq_get("WMT", get = "dividends", from = "2010-01-01")

wm_divs
## # A tibble: 46 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 36 more rows

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

Hint: Find the symbol in FRED.

us_un <- tq_get("UNRATE", get = "economic.data", from = "2010-01-01")

us_un
## # A tibble: 136 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 126 more rows

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

tq_get(c("FB", "NFLX", "GOOGL"), get = "stock.prices", from = "2019-01-01")
## # A tibble: 1,773 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,763 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.

Index <- tq_index("SP600")

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.

weight <- Index %>%
 select(weight) %>%
 mutate(
  weight = weight * 100,
)

weight
## # A tibble: 602 x 1
##    weight
##     <dbl>
##  1  0.840
##  2  0.662
##  3  0.595
##  4  0.574
##  5  0.526
##  6  0.515
##  7  0.513
##  8  0.468
##  9  0.467
## 10  0.462
## # ... with 592 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.

summarize(Index, weight = sum(weight))
## # A tibble: 1 x 1
##   weight
##    <dbl>
## 1   1.00

Q7 group_by and summarize Calculate the mean weight by sector. Which sector has the greatest weight on S&P600 on average?

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

Index %>% group_by(sector) %>% summarize(weight = sum(weight))
## # A tibble: 11 x 2
##    sector                 weight
##    <chr>                   <dbl>
##  1 Communication Services 0.0186
##  2 Consumer Discretionary 0.163 
##  3 Consumer Staples       0.0401
##  4 Energy                 0.0385
##  5 Financials             0.179 
##  6 Health Care            0.109 
##  7 Industrials            0.182 
##  8 Information Technology 0.125 
##  9 Materials              0.0541
## 10 Real Estate            0.0761
## 11 Utilities              0.0155

The Industrials sector has the greatest weight on the S&P600 with a weight of 0.182.

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.