Import stock prices
stocks <- tq_get(c("LULU", "NKE"),
get = "stock.prices",
from = "2024-01-01",
to = "2025-01-01")
stocks
## # A tibble: 504 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 LULU 2024-01-02 509. 509. 502. 505. 1187800 505.
## 2 LULU 2024-01-03 502. 504. 497. 498. 1029200 498.
## 3 LULU 2024-01-04 497. 502 495. 496 1174400 496
## 4 LULU 2024-01-05 496. 499. 491. 491. 1121500 491.
## 5 LULU 2024-01-08 488. 493. 478. 491. 1827300 491.
## 6 LULU 2024-01-09 489. 490. 483. 486. 1242700 486.
## 7 LULU 2024-01-10 488 489. 483. 484. 1276000 484.
## 8 LULU 2024-01-11 483. 486. 478. 485. 823500 485.
## 9 LULU 2024-01-12 488. 491. 479. 480. 1050100 480.
## 10 LULU 2024-01-16 478. 478. 468. 471. 1370200 471.
## # ℹ 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 > 502)
## # A tibble: 1 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 LULU 2024-01-02 509. 509. 502. 505. 1187800 505.
Arrange Rows
arrange(stocks, desc(x = date))
## # A tibble: 504 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 LULU 2024-12-31 384. 386. 380. 382. 1027600 382.
## 2 NKE 2024-12-31 74.9 76.4 74.8 75.7 9425600 75.3
## 3 LULU 2024-12-30 380. 389. 378. 384. 1254700 384.
## 4 NKE 2024-12-30 75.6 75.8 74.6 74.7 9042900 74.3
## 5 LULU 2024-12-27 382 387. 380. 387. 1075500 387.
## 6 NKE 2024-12-27 76.5 76.9 75.8 76.4 6334400 76.0
## 7 LULU 2024-12-26 385. 390. 384. 386. 1012900 386.
## 8 NKE 2024-12-26 76.6 77.5 76.5 76.9 6363500 76.6
## 9 LULU 2024-12-24 385. 387. 383. 386. 496400 386.
## 10 NKE 2024-12-24 76.8 77.1 76.4 76.8 4919100 76.4
## # ℹ 494 more rows
Add columns
mutate(stocks,
gain = high -low ) %>%
#Select date, high, low, and gain
select(date:high, gain)
## # A tibble: 504 × 4
## date open high gain
## <date> <dbl> <dbl> <dbl>
## 1 2024-01-02 509. 509. 6.77
## 2 2024-01-03 502. 504. 7.26
## 3 2024-01-04 497. 502 6.99
## 4 2024-01-05 496. 499. 8.52
## 5 2024-01-08 488. 493. 15.4
## 6 2024-01-09 489. 490. 6.84
## 7 2024-01-10 488 489. 5.92
## 8 2024-01-11 483. 486. 8.05
## 9 2024-01-12 488. 491. 11.8
## 10 2024-01-16 478. 478. 10.5
## # ℹ 494 more rows
# Just keep gain
mutate(stocks,
gain = high -low ) %>%
#Select date, high, low, and gain
select(gain)
## # A tibble: 504 × 1
## gain
## <dbl>
## 1 6.77
## 2 7.26
## 3 6.99
## 4 8.52
## 5 15.4
## 6 6.84
## 7 5.92
## 8 8.05
## 9 11.8
## 10 10.5
## # ℹ 494 more rows
Summarise with groups
stocks
## # A tibble: 504 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 LULU 2024-01-02 509. 509. 502. 505. 1187800 505.
## 2 LULU 2024-01-03 502. 504. 497. 498. 1029200 498.
## 3 LULU 2024-01-04 497. 502 495. 496 1174400 496
## 4 LULU 2024-01-05 496. 499. 491. 491. 1121500 491.
## 5 LULU 2024-01-08 488. 493. 478. 491. 1827300 491.
## 6 LULU 2024-01-09 489. 490. 483. 486. 1242700 486.
## 7 LULU 2024-01-10 488 489. 483. 484. 1276000 484.
## 8 LULU 2024-01-11 483. 486. 478. 485. 823500 485.
## 9 LULU 2024-01-12 488. 491. 479. 480. 1050100 480.
## 10 LULU 2024-01-16 478. 478. 468. 471. 1370200 471.
## # ℹ 494 more rows
# high
summarise(stocks, mean(high), na.rm =TRUE)
## # A tibble: 1 × 2
## `mean(high)` na.rm
## <dbl> <lgl>
## 1 220. TRUE