Import stock prices
stocks <- tq_get(c("GM", "TSLA", "HMC", "VWAGY", "F"),
get = "stock.prices",
from = "2020-01-01")
stocks
## # A tibble: 3,895 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 GM 2020-01-02 37 37.4 36.5 37.4 7454400 36.8
## 2 GM 2020-01-03 36.7 36.9 36.1 36.3 9173000 35.7
## 3 GM 2020-01-06 36.0 36.2 35.8 35.8 8408200 35.2
## 4 GM 2020-01-07 35.8 35.8 34.7 35.2 17556800 34.6
## 5 GM 2020-01-08 35.0 35.2 34.4 34.7 13229400 34.1
## 6 GM 2020-01-09 35.0 35.1 34.5 35.1 9610400 34.5
## 7 GM 2020-01-10 35.2 36.5 34.5 34.7 9644900 34.1
## 8 GM 2020-01-13 34.7 35 34.5 35.0 7465700 34.4
## 9 GM 2020-01-14 35 35.3 35.0 35.2 6827100 34.6
## 10 GM 2020-01-15 35 35.3 34.9 35.2 7173900 34.6
## # … with 3,885 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 > 25, adjusted < 35)
## # A tibble: 893 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 GM 2020-01-07 35.8 35.8 34.7 35.2 17556800 34.6
## 2 GM 2020-01-08 35.0 35.2 34.4 34.7 13229400 34.1
## 3 GM 2020-01-09 35.0 35.1 34.5 35.1 9610400 34.5
## 4 GM 2020-01-10 35.2 36.5 34.5 34.7 9644900 34.1
## 5 GM 2020-01-13 34.7 35 34.5 35.0 7465700 34.4
## 6 GM 2020-01-14 35 35.3 35.0 35.2 6827100 34.6
## 7 GM 2020-01-15 35 35.3 34.9 35.2 7173900 34.6
## 8 GM 2020-01-16 35.2 35.6 35.2 35.5 7280800 35.0
## 9 GM 2020-01-21 35.4 35.5 35.0 35.0 11550500 34.4
## 10 GM 2020-01-22 35.1 35.3 34.9 34.9 7358400 34.3
## # … with 883 more rows
Arrange rows
arrange(stocks, desc (high))
## # A tibble: 3,895 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 TSLA 2021-11-04 411. 414. 406. 410. 76192200 410.
## 2 TSLA 2021-11-05 409. 413. 403. 407. 64886400 407.
## 3 TSLA 2021-11-03 392. 405. 384. 405. 103885500 405.
## 4 TSLA 2021-11-01 382. 403. 373. 403. 168146100 403.
## 5 TSLA 2021-11-02 386. 403. 382 391. 128213400 391.
## 6 TSLA 2022-01-04 397. 403. 374. 383. 100248300 383.
## 7 TSLA 2021-11-22 387. 401. 377. 386. 99217500 386.
## 8 TSLA 2022-01-03 383. 400. 379. 400. 103931400 400.
## 9 TSLA 2021-11-08 383. 399 378. 388. 100337100 388.
## 10 TSLA 2021-11-23 389. 394. 354. 370. 108515100 370.
## # … with 3,885 more rows
arrange(stocks, desc (low))
## # A tibble: 3,895 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 TSLA 2021-11-04 411. 414. 406. 410. 76192200 410.
## 2 TSLA 2021-11-05 409. 413. 403. 407. 64886400 407.
## 3 TSLA 2021-11-03 392. 405. 384. 405. 103885500 405.
## 4 TSLA 2021-11-02 386. 403. 382 391. 128213400 391.
## 5 TSLA 2022-01-03 383. 400. 379. 400. 103931400 400.
## 6 TSLA 2021-11-08 383. 399 378. 388. 100337100 388.
## 7 TSLA 2021-11-22 387. 401. 377. 386. 99217500 386.
## 8 TSLA 2022-01-04 397. 403. 374. 383. 100248300 383.
## 9 TSLA 2021-11-01 382. 403. 373. 403. 168146100 403.
## 10 TSLA 2021-11-30 381. 389. 373. 382. 81276000 382.
## # … with 3,885 more rows
Select columns
select(stocks, symbol, open, close)
## # A tibble: 3,895 × 3
## symbol open close
## <chr> <dbl> <dbl>
## 1 GM 37 37.4
## 2 GM 36.7 36.3
## 3 GM 36.0 35.8
## 4 GM 35.8 35.2
## 5 GM 35.0 34.7
## 6 GM 35.0 35.1
## 7 GM 35.2 34.7
## 8 GM 34.7 35.0
## 9 GM 35 35.2
## 10 GM 35 35.2
## # … with 3,885 more rows
Add columns
mutate(stocks,
difference = close - open) %>%
select(symbol, open, close, difference)
## # A tibble: 3,895 × 4
## symbol open close difference
## <chr> <dbl> <dbl> <dbl>
## 1 GM 37 37.4 0.380
## 2 GM 36.7 36.3 -0.410
## 3 GM 36.0 35.8 -0.110
## 4 GM 35.8 35.2 -0.600
## 5 GM 35.0 34.7 -0.390
## 6 GM 35.0 35.1 0.0900
## 7 GM 35.2 34.7 -0.5
## 8 GM 34.7 35.0 0.290
## 9 GM 35 35.2 0.150
## 10 GM 35 35.2 0.150
## # … with 3,885 more rows
Summarise with groups
summarise(stocks, avg_open = mean(open, na.rm = TRUE))
## # A tibble: 1 × 1
## avg_open
## <dbl>
## 1 61.7
summarise(stocks, avg_close = mean(close, na.rm = TRUE))
## # A tibble: 1 × 1
## avg_close
## <dbl>
## 1 61.7
#summarise(stocks, avg_difference = mean(difference, na.rm = TRUE))
summarise(stocks, avg_adjusted = mean(adjusted, na.rm = TRUE))
## # A tibble: 1 × 1
## avg_adjusted
## <dbl>
## 1 60.7