Filter, by revising the code below.Suppose that you are weighing a few stocks for investment: Apple, Microsoft and Facebook. So you want to find out their historical performance in the past.
library(tidyquant)
library(ggplot2)
# Pick stocks
stocks <- c("AAPL", "MSFT" , "FB")
# Import stock prices
stock_prices <- stocks %>%
tq_get(get = "stock.prices",
from = "2013-01-01",
to = Sys.Date()) %>%
group_by(symbol)
stock_prices
## # A tibble: 4,125 x 8
## # Groups: symbol [3]
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2013-01-02 79.1 79.3 77.4 78.4 140129500 56.3
## 2 AAPL 2013-01-03 78.3 78.5 77.3 77.4 88241300 55.6
## 3 AAPL 2013-01-04 76.7 76.9 75.1 75.3 148583400 54.1
## 4 AAPL 2013-01-07 74.6 75.6 73.6 74.8 121039100 53.7
## 5 AAPL 2013-01-08 75.6 76.0 74.5 75.0 114676800 53.9
## 6 AAPL 2013-01-09 74.6 75.0 73.7 73.9 101901100 53.0
## 7 AAPL 2013-01-10 75.5 75.5 73.6 74.8 150286500 53.7
## 8 AAPL 2013-01-11 74.4 75.0 74.1 74.3 87626700 53.4
## 9 AAPL 2013-01-14 71.8 72.5 71.2 71.7 183551900 51.5
## 10 AAPL 2013-01-15 71.2 71.3 69.1 69.4 219193100 49.8
## # ... with 4,115 more rows
Filter, by revising the code below.# Plot one stock using filter
stock_prices %>%
filter(symbol == "FB") %>%
ggplot(aes(x = date, y = close)) +
geom_line()
To answer Q3 and Q4, use Arrange.
# Q3
stock_prices %>%
filter(symbol == "AAPL") %>%
arrange(desc(close))
## # A tibble: 1,375 x 8
## # Groups: symbol [1]
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2018-06-06 194. 194. 192. 194. 20933600 194.
## 2 AAPL 2018-06-07 194. 194. 192. 193. 21347200 193.
## 3 AAPL 2018-06-05 193. 194. 192. 193. 21566000 193.
## 4 AAPL 2018-06-12 191. 193. 191. 192. 16911100 192.
## 5 AAPL 2018-06-04 192. 193. 191. 192. 26132000 192.
## 6 AAPL 2018-06-08 191. 192 190. 192. 26656800 192.
## 7 AAPL 2018-06-11 191. 192. 190. 191. 18308500 191.
## 8 AAPL 2018-06-14 192. 192. 190. 191. 21610100 191.
## 9 AAPL 2018-06-13 192. 193. 190. 191. 21638400 191.
## 10 AAPL 2018-06-01 188. 190. 188. 190. 23250400 190.
## # ... with 1,365 more rows
# Q4
stock_prices %>%
filter(symbol == "AAPL") %>%
arrange(desc(date))
## # A tibble: 1,375 x 8
## # Groups: symbol [1]
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2018-06-18 188. 189. 187. 189. 14842280 189.
## 2 AAPL 2018-06-15 190. 190. 188. 189. 61289600 189.
## 3 AAPL 2018-06-14 192. 192. 190. 191. 21610100 191.
## 4 AAPL 2018-06-13 192. 193. 190. 191. 21638400 191.
## 5 AAPL 2018-06-12 191. 193. 191. 192. 16911100 192.
## 6 AAPL 2018-06-11 191. 192. 190. 191. 18308500 191.
## 7 AAPL 2018-06-08 191. 192 190. 192. 26656800 192.
## 8 AAPL 2018-06-07 194. 194. 192. 193. 21347200 193.
## 9 AAPL 2018-06-06 194. 194. 192. 194. 20933600 194.
## 10 AAPL 2018-06-05 193. 194. 192. 193. 21566000 193.
## # ... with 1,365 more rows
To answer this question, replace close by adjusted in the code below.
# Suppose that you invested $1 on the first day of the period.
stock_prices %>%
group_by(symbol) %>%
mutate(index = adjusted / adjusted[1]) %>%
ungroup() %>%
ggplot(aes(x = date, y = index, col = symbol)) +
geom_line()
stock_prices %>%
group_by(symbol) %>%
summarize(avg_daily = mode(adjusted))
## # A tibble: 3 x 2
## symbol avg_daily
## <chr> <chr>
## 1 AAPL numeric
## 2 FB numeric
## 3 MSFT numeric