library(tidyquant)
## Warning: package 'tidyquant' was built under R version 3.5.2
## Loading required package: lubridate
## Warning: package 'lubridate' was built under R version 3.5.2
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
## Loading required package: PerformanceAnalytics
## Warning: package 'PerformanceAnalytics' was built under R version 3.5.2
## Loading required package: xts
## Warning: package 'xts' was built under R version 3.5.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.5.2
##
## 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
## Warning: package 'quantmod' was built under R version 3.5.2
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 3.5.2
## Version 0.4-0 included new data defaults. See ?getSymbols.
## Loading required package: tidyverse
## Warning: package 'tidyverse' was built under R version 3.5.2
## -- Attaching packages -------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.0 v purrr 0.3.0
## v tibble 2.0.1 v dplyr 0.7.8
## v tidyr 0.8.2 v stringr 1.3.1
## v readr 1.3.1 v forcats 0.3.0
## Warning: package 'ggplot2' was built under R version 3.5.2
## Warning: package 'tibble' was built under R version 3.5.2
## Warning: package 'tidyr' was built under R version 3.5.2
## Warning: package 'readr' was built under R version 3.5.2
## Warning: package 'purrr' was built under R version 3.5.2
## Warning: package 'dplyr' was built under R version 3.5.2
## Warning: package 'stringr' was built under R version 3.5.2
## Warning: package 'forcats' was built under R version 3.5.2
## -- 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()
library(tidyverse)
stocks <- tq_get("AAPL", get = "stock.prices", from = "2016-01-01")
## Warning: package 'bindrcpp' was built under R version 3.5.2
stocks %>% View()
stocks %>%
ggplot(aes(x = date, y = close)) +
geom_line()
## Q1. How many columns (variables) are there? There are seven columns
date open high low close volume adjusted
stocks <- tq_get(c("AAPL", "MSFT"), get = "stock.prices", from = "2016-01-01")
stocks
## # A tibble: 1,568 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2016-01-04 103. 105. 102 105. 67649400 99.5
## 2 AAPL 2016-01-05 106. 106. 102. 103. 55791000 97.0
## 3 AAPL 2016-01-06 101. 102. 99.9 101. 68457400 95.1
## 4 AAPL 2016-01-07 98.7 100. 96.4 96.4 81094400 91.1
## 5 AAPL 2016-01-08 98.6 99.1 96.8 97.0 70798000 91.6
## 6 AAPL 2016-01-11 99.0 99.1 97.3 98.5 49739400 93.1
## 7 AAPL 2016-01-12 101. 101. 98.8 100.0 49154200 94.4
## 8 AAPL 2016-01-13 100. 101. 97.3 97.4 62439600 92.0
## 9 AAPL 2016-01-14 98.0 100. 95.7 99.5 63170100 94.0
## 10 AAPL 2016-01-15 96.2 97.7 95.4 97.1 79010000 91.7
## # ... with 1,558 more rows
Yes there are 8 variables and the new variable is symbol
Hint: Use dplyr::mutate. Market cap is given by the formula, MC = N × P, where MC is the market capitalization, N is the number of shares outstanding, and P is the closing price per share.
stocks %>%
mutate(MC = volume * close)
## # A tibble: 1,568 x 9
## symbol date open high low close volume adjusted MC
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2016-01-04 103. 105. 102 105. 67649400 99.5 7126864155.
## 2 AAPL 2016-01-05 106. 106. 102. 103. 55791000 97.0 5730293554.
## 3 AAPL 2016-01-06 101. 102. 99.9 101. 68457400 95.1 6893659975.
## 4 AAPL 2016-01-07 98.7 100. 96.4 96.4 81094400 91.1 7821554637.
## 5 AAPL 2016-01-08 98.6 99.1 96.8 97.0 70798000 91.6 6864574009.
## 6 AAPL 2016-01-11 99.0 99.1 97.3 98.5 49739400 93.1 4900823032.
## 7 AAPL 2016-01-12 101. 101. 98.8 100.0 49154200 94.4 4913453783.
## 8 AAPL 2016-01-13 100. 101. 97.3 97.4 62439600 92.0 6080992582.
## 9 AAPL 2016-01-14 98.0 100. 95.7 99.5 63170100 94.0 6286688162.
## 10 AAPL 2016-01-15 96.2 97.7 95.4 97.1 79010000 91.7 7674241063.
## # ... with 1,558 more rows
Hint: Use dplyr::select.
stocks %>% select(symbol, date,)
## # A tibble: 1,568 x 2
## symbol date
## <chr> <date>
## 1 AAPL 2016-01-04
## 2 AAPL 2016-01-05
## 3 AAPL 2016-01-06
## 4 AAPL 2016-01-07
## 5 AAPL 2016-01-08
## 6 AAPL 2016-01-11
## 7 AAPL 2016-01-12
## 8 AAPL 2016-01-13
## 9 AAPL 2016-01-14
## 10 AAPL 2016-01-15
## # ... with 1,558 more rows
Hint: Use ggplot2::ggplot.
stocks %>%
ggplot(aes(x = date, y = close, color = symbol)) +
geom_line()