# Load packages
library(tidyquant)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
## == Need to Learn tidyquant? ==========================================================================================
## Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
## </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
library(tidyverse) # for count() function
## -- Attaching packages ----------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2 v purrr 0.3.4
## v tibble 3.0.3 v dplyr 1.0.2
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts -------------------------------------------------------------------------------- tidyverse_conflicts() --
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date() masks base::date()
## x dplyr::filter() masks stats::filter()
## x dplyr::first() masks xts::first()
## x lubridate::intersect() masks base::intersect()
## x dplyr::lag() masks stats::lag()
## x dplyr::last() masks xts::last()
## x lubridate::setdiff() masks base::setdiff()
## x lubridate::union() masks base::union()
# Import S&P500 Stock Index
SP500 <- tq_index("SP500")
## Getting holdings for SP500
SP500
## # A tibble: 505 x 8
## symbol company identifier sedol weight sector shares_held local_currency
## <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr>
## 1 AAPL Apple Inc. 03783310 20462~ 0.0667 Inform~ 174650100 USD
## 2 MSFT Microsoft~ 59491810 25881~ 0.0558 Inform~ 81274030 USD
## 3 AMZN Amazon.co~ 02313510 20000~ 0.0479 Consum~ 4490004 USD
## 4 FB Facebook ~ 30303M10 B7TL8~ 0.0237 Commun~ 25768718 USD
## 5 GOOGL Alphabet ~ 02079K30 BYVY8~ 0.0166 Commun~ 3218310 USD
## 6 GOOG Alphabet ~ 02079K10 BYY88~ 0.0163 Commun~ 3134044 USD
## 7 BRK.B Berkshire~ 08467070 20733~ 0.0154 Financ~ 20853808 USD
## 8 JNJ Johnson &~ 47816010 24758~ 0.0141 Health~ 28258660 USD
## 9 V Visa Inc.~ 92826C83 B2PZN~ 0.0123 Inform~ 18095848 USD
## 10 PG Procter &~ 74271810 27044~ 0.0122 Consum~ 26545804 USD
## # ... with 495 more rows
505 rows
8 columns
505
Hint: Use the weight column of the data. Apple, Microsoft, and Amazon
Hint: Use the sector column of the data. Information technology, health care, financials, and consumer discretionary
Hint: Use the sector column of the data. NFLX
Hint: Insert a R code chunk below, type the following code: count(SP500, sector, sort = T)
count(SP500, sector, sort = T)
## # A tibble: 11 x 2
## sector n
## <chr> <int>
## 1 Industrials 73
## 2 Information Technology 71
## 3 Financials 66
## 4 Health Care 62
## 5 Consumer Discretionary 61
## 6 Consumer Staples 33
## 7 Real Estate 31
## 8 Materials 28
## 9 Utilities 28
## 10 Communication Services 26
## 11 Energy 26
Industrials
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.