# Load packages
library(tidyquant)
library(tidyverse) # for count() function
# Import S&P500 Stock Index
SP500 <- tq_index("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.0642 Inform~ 168308740 USD
## 2 MSFT Microsoft~ 59491810 25881~ 0.0566 Inform~ 79227850 USD
## 3 AMZN Amazon.co~ 02313510 20000~ 0.0476 Consum~ 4457388 USD
## 4 FB Facebook ~ 30303M10 B7TL8~ 0.0223 Commun~ 25171544 USD
## 5 GOOGL Alphabet ~ 02079K30 BYVY8~ 0.0158 Commun~ 3145616 USD
## 6 BRK.B Berkshire~ 08467070 20733~ 0.0155 Financ~ 20752296 USD
## 7 GOOG Alphabet ~ 02079K10 BYY88~ 0.0155 Commun~ 3073503 USD
## 8 JNJ Johnson &~ 47816010 24758~ 0.0142 Health~ 27563788 USD
## 9 PG Procter &~ 74271810 27044~ 0.0127 Consum~ 26064468 USD
## 10 V Visa Inc.~ 92826C83 B2PZN~ 0.0123 Inform~ 17651196 USD
## # ... with 495 more rows
The data set approximately 505 rows
The data set has 8 columns
There are 505 different companies on the s&p500 Index
Hint: Use the weight column of the data. Microsoft, Amazon, and Apple have the most influence on the S&P500 Index
Hint: Use the sector column of the data. Four sectors of the S&P500 index is Consumer Discretionary, Information Technology, Financial, Healthcare
Hint: Use the symbol column of the data. The symbol for Netflix is 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 72
## 3 Financials 66
## 4 Health Care 63
## 5 Consumer Discretionary 60
## 6 Consumer Staples 32
## 7 Real Estate 31
## 8 Materials 28
## 9 Utilities 28
## 10 Communication Services 26
## 11 Energy 26
Industrials has the bigges representation in the Index ## Q8 Hide the messages, but display the code and its results on the webpage. Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.