Import stock prices
stocks <- tq_get(c("JNJ", "KSS", "TDS"),
get = "stock.prices",
from = "2017-01-01",
to = "2019-01-01")
stocks
## # A tibble: 1,506 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 JNJ 2017-01-03 116. 116 115. 116. 5953000 93.8
## 2 JNJ 2017-01-04 116. 116. 115. 116. 5828900 93.7
## 3 JNJ 2017-01-05 116 117. 115. 117. 6217200 94.6
## 4 JNJ 2017-01-06 117. 117 116. 116. 5221400 94.2
## 5 JNJ 2017-01-09 116. 117. 116. 116. 5457500 94.2
## 6 JNJ 2017-01-10 116. 117 116. 116. 5312800 94.1
## 7 JNJ 2017-01-11 116. 116. 114. 115. 8887200 92.9
## 8 JNJ 2017-01-12 114. 115. 113. 115. 6627300 92.8
## 9 JNJ 2017-01-13 115. 115. 114. 115. 4935200 92.8
## 10 JNJ 2017-01-17 114. 115. 114. 115. 6255400 93.0
## # ℹ 1,496 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: 624 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 JNJ 2017-01-03 116. 116 115. 116. 5953000 93.8
## 2 JNJ 2017-01-04 116. 116. 115. 116. 5828900 93.7
## 3 JNJ 2017-01-05 116 117. 115. 117. 6217200 94.6
## 4 JNJ 2017-01-06 117. 117 116. 116. 5221400 94.2
## 5 JNJ 2017-01-09 116. 117. 116. 116. 5457500 94.2
## 6 JNJ 2017-01-10 116. 117 116. 116. 5312800 94.1
## 7 JNJ 2017-01-11 116. 116. 114. 115. 8887200 92.9
## 8 JNJ 2017-01-12 114. 115. 113. 115. 6627300 92.8
## 9 JNJ 2017-01-13 115. 115. 114. 115. 4935200 92.8
## 10 JNJ 2017-01-17 114. 115. 114. 115. 6255400 93.0
## # ℹ 614 more rows
Arrange Rows
stocks %>% arrange (open)
## # A tibble: 1,506 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 TDS 2018-07-26 24.1 25.2 24.0 24.9 693200 19.9
## 2 TDS 2018-02-14 24.2 24.5 24.0 24.4 505700 19.2
## 3 TDS 2018-02-12 24.3 24.7 24.2 24.5 452300 19.2
## 4 TDS 2018-07-25 24.3 24.3 24 24.1 715100 19.2
## 5 TDS 2018-02-13 24.3 24.4 23.9 24.3 449200 19.1
## 6 TDS 2018-07-24 24.4 24.4 24.0 24.2 481900 19.3
## 7 TDS 2018-07-23 24.4 24.4 24.1 24.3 383800 19.4
## 8 TDS 2018-07-20 24.5 24.5 24 24.4 708500 19.4
## 9 TDS 2018-02-09 24.6 24.7 23.5 24.3 750400 19.1
## 10 TDS 2018-02-15 24.6 25.5 24.3 25.5 524300 20.0
## # ℹ 1,496 more rows
Select Columns
stocks %>% select(date,high)
## # A tibble: 1,506 × 2
## date high
## <date> <dbl>
## 1 2017-01-03 116
## 2 2017-01-04 116.
## 3 2017-01-05 117.
## 4 2017-01-06 117
## 5 2017-01-09 117.
## 6 2017-01-10 117
## 7 2017-01-11 116.
## 8 2017-01-12 115.
## 9 2017-01-13 115.
## 10 2017-01-17 115.
## # ℹ 1,496 more rows
Add Columns
stocks %>% select (date, high, open)
## # A tibble: 1,506 × 3
## date high open
## <date> <dbl> <dbl>
## 1 2017-01-03 116 116.
## 2 2017-01-04 116. 116.
## 3 2017-01-05 117. 116
## 4 2017-01-06 117 117.
## 5 2017-01-09 117. 116.
## 6 2017-01-10 117 116.
## 7 2017-01-11 116. 116.
## 8 2017-01-12 115. 114.
## 9 2017-01-13 115. 115.
## 10 2017-01-17 115. 114.
## # ℹ 1,496 more rows
mutate(stocks, gain =low)
## # A tibble: 1,506 × 9
## symbol date open high low close volume adjusted gain
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 JNJ 2017-01-03 116. 116 115. 116. 5953000 93.8 115.
## 2 JNJ 2017-01-04 116. 116. 115. 116. 5828900 93.7 115.
## 3 JNJ 2017-01-05 116 117. 115. 117. 6217200 94.6 115.
## 4 JNJ 2017-01-06 117. 117 116. 116. 5221400 94.2 116.
## 5 JNJ 2017-01-09 116. 117. 116. 116. 5457500 94.2 116.
## 6 JNJ 2017-01-10 116. 117 116. 116. 5312800 94.1 116.
## 7 JNJ 2017-01-11 116. 116. 114. 115. 8887200 92.9 114.
## 8 JNJ 2017-01-12 114. 115. 113. 115. 6627300 92.8 113.
## 9 JNJ 2017-01-13 115. 115. 114. 115. 4935200 92.8 114.
## 10 JNJ 2017-01-17 114. 115. 114. 115. 6255400 93.0 114.
## # ℹ 1,496 more rows
Summarize with groups
stocks %>% summarise (volume)
## 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: 1,506 × 1
## volume
## <dbl>
## 1 5953000
## 2 5828900
## 3 6217200
## 4 5221400
## 5 5457500
## 6 5312800
## 7 8887200
## 8 6627300
## 9 4935200
## 10 6255400
## # ℹ 1,496 more rows