dividends Import dividends of Costco and Target since 2000.economic data Import the U.S. Industrial Production Index since 2010.stock prices Import stock prices of NASDAQ, Dow Jones Industrial Average, and S&P500 since 2019.stock index Get all stocks in the Dow Jones Industrial Avarage (DOW) Index using tq_index().mutate Convert weight in decimals to percentages.summarize You would think all values in weight should sum to 100%. Calculate the sum of weight to confirm.group_by and summarize Calculate the mean weight by sector. Which sector has the greatest weight on DOW on average?# Load packages
library(tidyquant)
library(tidyverse)
dividends Import dividends of Costco and Target since 2000. dividends <-
tq_get(c("COST", "TGT"),
get = "dividends",
from = "2000-01-01")
dividends
## # A tibble: 152 x 3
## symbol date value
## <chr> <date> <dbl>
## 1 COST 2004-05-06 0.1
## 2 COST 2004-07-21 0.1
## 3 COST 2004-11-03 0.1
## 4 COST 2005-02-04 0.1
## 5 COST 2005-05-04 0.115
## 6 COST 2005-08-03 0.115
## 7 COST 2005-11-16 0.115
## 8 COST 2006-02-07 0.115
## 9 COST 2006-05-08 0.13
## 10 COST 2006-07-28 0.13
## # ... with 142 more rows
economic data Import the U.S. Industrial Production Index since 2010.Hint: Find the symbol in FRED.
dividends <-
tq_get(c("COST", "TGT"),
get = "dividends",
from = "2000-01-01")
dividends
## # A tibble: 152 x 3
## symbol date value
## <chr> <date> <dbl>
## 1 COST 2004-05-06 0.1
## 2 COST 2004-07-21 0.1
## 3 COST 2004-11-03 0.1
## 4 COST 2005-02-04 0.1
## 5 COST 2005-05-04 0.115
## 6 COST 2005-08-03 0.115
## 7 COST 2005-11-16 0.115
## 8 COST 2006-02-07 0.115
## 9 COST 2006-05-08 0.13
## 10 COST 2006-07-28 0.13
## # ... with 142 more rows
stock prices Import stock prices of NASDAQ, Dow Jones Industrial Average, and S&P500 since 2019.tq_get(c("^IXIC", "^DJI","^GSPC"),
get = "stock.prices",
from = "2019-01-01")
## # A tibble: 1,335 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^IXIC 2019-01-02 6507. 6694. 6507. 6666. 2261800000 6666.
## 2 ^IXIC 2019-01-03 6585. 6600. 6457. 6464. 2607290000 6464.
## 3 ^IXIC 2019-01-04 6567. 6761. 6554. 6739. 2579550000 6739.
## 4 ^IXIC 2019-01-07 6758. 6856. 6741. 6823. 2507550000 6823.
## 5 ^IXIC 2019-01-08 6893. 6910. 6796. 6897 2380290000 6897
## 6 ^IXIC 2019-01-09 6923. 6985. 6900. 6957. 2422590000 6957.
## 7 ^IXIC 2019-01-10 6909. 6991. 6877. 6986. 2179080000 6986.
## 8 ^IXIC 2019-01-11 6947. 6976. 6934. 6971. 2066500000 6971.
## 9 ^IXIC 2019-01-14 6908. 6936. 6887. 6906. 1942210000 6906.
## 10 ^IXIC 2019-01-15 6931. 7026. 6928. 7024. 2038090000 7024.
## # ... with 1,325 more rows
stock index Get all stocks in the Dow Jones Industrial Avarage (DOW) 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.
DOW <- tq_index("DOW")
DOW
## # A tibble: 30 x 8
## symbol company identifier sedol weight sector shares_held local_currency
## <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr>
## 1 UNH UnitedHea~ 91324P10 29177~ 0.0745 Health~ 5431277 USD
## 2 HD Home Depo~ 43707610 24342~ 0.0655 Consum~ 5431277 USD
## 3 AMGN Amgen Inc. 03116210 20236~ 0.0599 Health~ 5431277 USD
## 4 CRM salesforc~ 79466L30 23105~ 0.0593 Inform~ 5431277 USD
## 5 MCD McDonald'~ 58013510 25507~ 0.0531 Consum~ 5431277 USD
## 6 MSFT Microsoft~ 59491810 25881~ 0.0488 Inform~ 5431277 USD
## 7 GS Goldman S~ 38141G10 24079~ 0.0476 Financ~ 5431277 USD
## 8 V Visa Inc.~ 92826C83 B2PZN~ 0.0475 Inform~ 5431277 USD
## 9 HON Honeywell~ 43851610 20204~ 0.0395 Indust~ 5431277 USD
## 10 MMM 3M Company 88579Y10 25957~ 0.0384 Indust~ 5431277 USD
## # ... with 20 more rows
mutate Convert weight in decimals to percentages.Hint: Multiply weight by 100 and save it under the same name, weight. You may Google “dplyr::mutate” to find its documentation.
DOW <-
DOW %>%
mutate(weight = weight * 100)
DOW
## # A tibble: 30 x 8
## symbol company identifier sedol weight sector shares_held local_currency
## <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr>
## 1 UNH UnitedHea~ 91324P10 29177~ 7.45 Health~ 5431277 USD
## 2 HD Home Depo~ 43707610 24342~ 6.55 Consum~ 5431277 USD
## 3 AMGN Amgen Inc. 03116210 20236~ 5.99 Health~ 5431277 USD
## 4 CRM salesforc~ 79466L30 23105~ 5.93 Inform~ 5431277 USD
## 5 MCD McDonald'~ 58013510 25507~ 5.31 Consum~ 5431277 USD
## 6 MSFT Microsoft~ 59491810 25881~ 4.88 Inform~ 5431277 USD
## 7 GS Goldman S~ 38141G10 24079~ 4.76 Financ~ 5431277 USD
## 8 V Visa Inc.~ 92826C83 B2PZN~ 4.75 Inform~ 5431277 USD
## 9 HON Honeywell~ 43851610 20204~ 3.95 Indust~ 5431277 USD
## 10 MMM 3M Company 88579Y10 25957~ 3.84 Indust~ 5431277 USD
## # ... with 20 more rows
summarize You would think all values in weight should sum to 100%. Calculate the sum of weight to confirm.Hint: You may Google “dplyr::summarize” to find its documentation.
DOW %>%
summarize(sum = sum(weight))
## # A tibble: 1 x 1
## sum
## <dbl>
## 1 100
group_by and summarize Calculate the mean weight by sector. Which sector has the greatest weight on DOW on average?Hint: You may Google “dplyr::group_by” to find its documentation.
DOW %>%
group_by(sector) %>%
summarize(mean = mean(weight))
## # A tibble: 9 x 2
## sector mean
## <chr> <dbl>
## 1 Communication Services 2.14
## 2 Consumer Discretionary 4.96
## 3 Consumer Staples 2.16
## 4 Energy 1.71
## 5 Financials 3.03
## 6 Health Care 4.70
## 7 Industrials 3.79
## 8 Information Technology 3.32
## 9 Materials 1.13
Hint: Refer to the RMarkdown Reference Guide.