Import stock prices
stocks <- tq_get(c("AVGO", "AMD", "CRWD"),
get = "stock.prices",
from = "2022-10-25",
to = "2024-03-15")
stocks
## # A tibble: 1,044 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AVGO 2022-10-25 45.8 46.4 45.7 46.0 18246000 44.3
## 2 AVGO 2022-10-26 45.8 47.2 45.5 46.1 29291000 44.4
## 3 AVGO 2022-10-27 46.6 47.1 45.5 45.5 18512000 43.8
## 4 AVGO 2022-10-28 45.7 47.4 45.6 47.3 23094000 45.5
## 5 AVGO 2022-10-31 46.7 47.3 46.5 47.0 19259000 45.2
## 6 AVGO 2022-11-01 47.6 47.7 46.3 46.8 16324000 45.0
## 7 AVGO 2022-11-02 47.0 48.0 45.6 45.6 21070000 43.9
## 8 AVGO 2022-11-03 45.1 45.3 44.1 44.4 23887000 42.7
## 9 AVGO 2022-11-04 45.7 46.9 45.3 46.5 28891000 44.8
## 10 AVGO 2022-11-07 46.7 47.8 46.2 47.5 17687000 45.7
## # ℹ 1,034 more rows
Plot stock prices
stocks %>%
ggplot(aes(x = date, y = adjusted, color = symbol)) +
geom_line()

Filter Rows
filter(stocks, symbol == "AVGO")
## # A tibble: 348 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AVGO 2022-10-25 45.8 46.4 45.7 46.0 18246000 44.3
## 2 AVGO 2022-10-26 45.8 47.2 45.5 46.1 29291000 44.4
## 3 AVGO 2022-10-27 46.6 47.1 45.5 45.5 18512000 43.8
## 4 AVGO 2022-10-28 45.7 47.4 45.6 47.3 23094000 45.5
## 5 AVGO 2022-10-31 46.7 47.3 46.5 47.0 19259000 45.2
## 6 AVGO 2022-11-01 47.6 47.7 46.3 46.8 16324000 45.0
## 7 AVGO 2022-11-02 47.0 48.0 45.6 45.6 21070000 43.9
## 8 AVGO 2022-11-03 45.1 45.3 44.1 44.4 23887000 42.7
## 9 AVGO 2022-11-04 45.7 46.9 45.3 46.5 28891000 44.8
## 10 AVGO 2022-11-07 46.7 47.8 46.2 47.5 17687000 45.7
## # ℹ 338 more rows
Arrange Rows
arrange(stocks, desc(date), desc(volume))
## # A tibble: 1,044 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AMD 2024-03-14 193. 197. 184. 187. 84490500 187.
## 2 AVGO 2024-03-14 126. 127. 125. 126. 38029000 125.
## 3 CRWD 2024-03-14 333 338. 325. 329. 2776800 329.
## 4 AMD 2024-03-13 199. 199. 193. 195. 70526900 195.
## 5 AVGO 2024-03-13 128. 128. 125. 126. 37838000 125.
## 6 CRWD 2024-03-13 330. 335. 326. 332. 2730900 332.
## 7 AMD 2024-03-12 201. 203. 194. 203. 68951700 203.
## 8 AVGO 2024-03-12 131. 131. 126. 129. 42789000 128.
## 9 CRWD 2024-03-12 319. 334. 317. 329. 4156500 329.
## 10 AMD 2024-03-11 203. 204. 197. 198. 73098600 198.
## # ℹ 1,034 more rows
Select Columns
select(stocks, symbol: close)
## # A tibble: 1,044 × 6
## symbol date open high low close
## <chr> <date> <dbl> <dbl> <dbl> <dbl>
## 1 AVGO 2022-10-25 45.8 46.4 45.7 46.0
## 2 AVGO 2022-10-26 45.8 47.2 45.5 46.1
## 3 AVGO 2022-10-27 46.6 47.1 45.5 45.5
## 4 AVGO 2022-10-28 45.7 47.4 45.6 47.3
## 5 AVGO 2022-10-31 46.7 47.3 46.5 47.0
## 6 AVGO 2022-11-01 47.6 47.7 46.3 46.8
## 7 AVGO 2022-11-02 47.0 48.0 45.6 45.6
## 8 AVGO 2022-11-03 45.1 45.3 44.1 44.4
## 9 AVGO 2022-11-04 45.7 46.9 45.3 46.5
## 10 AVGO 2022-11-07 46.7 47.8 46.2 47.5
## # ℹ 1,034 more rows
select(stocks, symbol, date, open)
## # A tibble: 1,044 × 3
## symbol date open
## <chr> <date> <dbl>
## 1 AVGO 2022-10-25 45.8
## 2 AVGO 2022-10-26 45.8
## 3 AVGO 2022-10-27 46.6
## 4 AVGO 2022-10-28 45.7
## 5 AVGO 2022-10-31 46.7
## 6 AVGO 2022-11-01 47.6
## 7 AVGO 2022-11-02 47.0
## 8 AVGO 2022-11-03 45.1
## 9 AVGO 2022-11-04 45.7
## 10 AVGO 2022-11-07 46.7
## # ℹ 1,034 more rows
Add Columns
mutate(stocks,
day_diff = close - open) %>%
select(symbol:close, day_diff)
## # A tibble: 1,044 × 7
## symbol date open high low close day_diff
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AVGO 2022-10-25 45.8 46.4 45.7 46.0 0.208
## 2 AVGO 2022-10-26 45.8 47.2 45.5 46.1 0.319
## 3 AVGO 2022-10-27 46.6 47.1 45.5 45.5 -1.06
## 4 AVGO 2022-10-28 45.7 47.4 45.6 47.3 1.59
## 5 AVGO 2022-10-31 46.7 47.3 46.5 47.0 0.327
## 6 AVGO 2022-11-01 47.6 47.7 46.3 46.8 -0.790
## 7 AVGO 2022-11-02 47.0 48.0 45.6 45.6 -1.42
## 8 AVGO 2022-11-03 45.1 45.3 44.1 44.4 -0.682
## 9 AVGO 2022-11-04 45.7 46.9 45.3 46.5 0.844
## 10 AVGO 2022-11-07 46.7 47.8 46.2 47.5 0.845
## # ℹ 1,034 more rows
Summarize By Group
stocks %>%
group_by(symbol) %>%
summarise(avg_volume = mean(volume, na.rm = TRUE)) %>%
arrange(avg_volume)
## # A tibble: 3 × 2
## symbol avg_volume
## <chr> <dbl>
## 1 CRWD 4131494.
## 2 AVGO 26594184.
## 3 AMD 65180555.