Import stock prices
stocks <- tq_get(c("NKE", "ADDYY"),
get = "stock.prices",
from = "2016-01-01",
to = "2017-01-01")
stocks
## # A tibble: 504 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NKE 2016-01-04 61.1 61.9 60.9 61.5 11626800 54.8
## 2 NKE 2016-01-05 61.7 62.6 61.7 62.4 9220600 55.5
## 3 NKE 2016-01-06 61.3 62.0 61.2 61.5 6551600 54.8
## 4 NKE 2016-01-07 60.4 61.3 59.8 59.8 10881300 53.3
## 5 NKE 2016-01-08 60.1 60.8 58.7 58.9 11191300 52.4
## 6 NKE 2016-01-11 59.0 60.0 58.5 59.5 12825000 53.0
## 7 NKE 2016-01-12 60.3 60.8 59.6 59.9 8292200 53.4
## 8 NKE 2016-01-13 60.5 60.5 58.7 58.8 9944300 52.3
## 9 NKE 2016-01-14 59 59.3 57.3 58.5 9989000 52.1
## 10 NKE 2016-01-15 56.9 58.1 56.6 57.6 12208300 51.3
## # ℹ 494 more rows
Plot stock prices
stocks %>%
ggplot(aes(x = date, y = adjusted, color = symbol)) +
geom_line()

Apply the dplyr verbs you learned in chapter 5
Filter rows
stocks %>% filter(adjusted > 50)
## # A tibble: 341 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NKE 2016-01-04 61.1 61.9 60.9 61.5 11626800 54.8
## 2 NKE 2016-01-05 61.7 62.6 61.7 62.4 9220600 55.5
## 3 NKE 2016-01-06 61.3 62.0 61.2 61.5 6551600 54.8
## 4 NKE 2016-01-07 60.4 61.3 59.8 59.8 10881300 53.3
## 5 NKE 2016-01-08 60.1 60.8 58.7 58.9 11191300 52.4
## 6 NKE 2016-01-11 59.0 60.0 58.5 59.5 12825000 53.0
## 7 NKE 2016-01-12 60.3 60.8 59.6 59.9 8292200 53.4
## 8 NKE 2016-01-13 60.5 60.5 58.7 58.8 9944300 52.3
## 9 NKE 2016-01-14 59 59.3 57.3 58.5 9989000 52.1
## 10 NKE 2016-01-15 56.9 58.1 56.6 57.6 12208300 51.3
## # ℹ 331 more rows
Arrange rows
stocks %>% arrange(low, high)
## # A tibble: 504 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ADDYY 2016-01-08 45.9 45.9 45.1 45.1 32600 41.2
## 2 ADDYY 2016-01-15 45.5 46.1 45.4 45.5 77100 41.6
## 3 ADDYY 2016-01-07 45.8 46.1 45.7 45.8 42400 41.9
## 4 ADDYY 2016-01-13 46.2 46.5 45.8 45.9 45000 41.9
## 5 ADDYY 2016-01-14 46.0 46.6 45.8 46.3 41800 42.3
## 6 ADDYY 2016-01-06 46.0 46.4 46.0 46.3 61300 42.3
## 7 ADDYY 2016-01-11 46.7 46.7 46.2 46.7 52400 42.6
## 8 ADDYY 2016-01-12 47.0 47.1 46.6 47.1 74000 43.0
## 9 ADDYY 2016-01-05 46.9 47.1 46.7 47.1 41400 43.0
## 10 ADDYY 2016-01-04 47.6 47.8 47 47.8 76000 43.6
## # ℹ 494 more rows
Select columns
stocks %>% select(high)
## # A tibble: 504 × 1
## high
## <dbl>
## 1 61.9
## 2 62.6
## 3 62.0
## 4 61.3
## 5 60.8
## 6 60.0
## 7 60.8
## 8 60.5
## 9 59.3
## 10 58.1
## # ℹ 494 more rows
Add columns
mutate(stocks,
gain = high) %>%
select(gain)
## # A tibble: 504 × 1
## gain
## <dbl>
## 1 61.9
## 2 62.6
## 3 62.0
## 4 61.3
## 5 60.8
## 6 60.0
## 7 60.8
## 8 60.5
## 9 59.3
## 10 58.1
## # ℹ 494 more rows
Summarise with groups
stocks %>%
summarise(high)
## Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
## dplyr 1.1.0.
## ℹ Please use `reframe()` instead.
## ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
## always returns an ungrouped data frame and adjust accordingly.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## # A tibble: 504 × 1
## high
## <dbl>
## 1 61.9
## 2 62.6
## 3 62.0
## 4 61.3
## 5 60.8
## 6 60.0
## 7 60.8
## 8 60.5
## 9 59.3
## 10 58.1
## # ℹ 494 more rows