Import stock prices
stocks <- tq_get(c("HMC", "WMT", "TGT"),
get = "stock.prices",
from = "2016-01-01",
to = "2023-01-01")
stocks
## # A tibble: 5,286 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 HMC 2016-01-04 30.9 31 30.7 31.0 760600 26.2
## 2 HMC 2016-01-05 30.8 30.9 30.7 30.8 517100 26.0
## 3 HMC 2016-01-06 30.4 30.4 30.1 30.2 433100 25.6
## 4 HMC 2016-01-07 29.8 29.9 29.4 29.5 650600 25.0
## 5 HMC 2016-01-08 29.9 30.0 28.8 28.9 862500 24.4
## 6 HMC 2016-01-11 29.2 29.4 29.0 29.4 943600 24.8
## 7 HMC 2016-01-12 30.1 30.1 29.5 29.9 897900 25.3
## 8 HMC 2016-01-13 30.3 30.3 29.6 29.7 680200 25.1
## 9 HMC 2016-01-14 29.7 30.1 29.4 30.0 705900 25.3
## 10 HMC 2016-01-15 29.1 29.3 28.8 29.0 572900 24.6
## # … with 5,276 more rows
Plot stock prices
stocks %>%
ggplot(aes(x = date, y = adjusted, color = symbol)) +
geom_line()
Apply the dp1yr Verbs you Learned in Chapter 5
Filter rows
stocks %>% filter(adjusted > 26)
## # A tibble: 4,266 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 HMC 2016-01-04 30.9 31 30.7 31.0 760600 26.2
## 2 HMC 2016-01-05 30.8 30.9 30.7 30.8 517100 26.0
## 3 HMC 2016-08-19 30.7 30.7 30.5 30.6 1138500 26.3
## 4 HMC 2016-08-22 30.9 31.1 30.9 31.0 1056700 26.6
## 5 HMC 2016-08-23 30.7 30.8 30.6 30.6 529800 26.2
## 6 HMC 2016-08-24 30.7 30.8 30.5 30.6 459100 26.3
## 7 HMC 2016-08-25 30.6 30.7 30.5 30.6 411300 26.3
## 8 HMC 2016-08-29 30.5 30.7 30.5 30.6 521800 26.3
## 9 HMC 2016-08-30 30.9 31.0 30.8 30.9 497600 26.5
## 10 HMC 2016-08-31 30.8 30.8 30.7 30.8 463200 26.4
## # … with 4,256 more rows
Arrange rows
stocks %>% arrange(date())
## # A tibble: 5,286 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 HMC 2016-01-04 30.9 31 30.7 31.0 760600 26.2
## 2 HMC 2016-01-05 30.8 30.9 30.7 30.8 517100 26.0
## 3 HMC 2016-01-06 30.4 30.4 30.1 30.2 433100 25.6
## 4 HMC 2016-01-07 29.8 29.9 29.4 29.5 650600 25.0
## 5 HMC 2016-01-08 29.9 30.0 28.8 28.9 862500 24.4
## 6 HMC 2016-01-11 29.2 29.4 29.0 29.4 943600 24.8
## 7 HMC 2016-01-12 30.1 30.1 29.5 29.9 897900 25.3
## 8 HMC 2016-01-13 30.3 30.3 29.6 29.7 680200 25.1
## 9 HMC 2016-01-14 29.7 30.1 29.4 30.0 705900 25.3
## 10 HMC 2016-01-15 29.1 29.3 28.8 29.0 572900 24.6
## # … with 5,276 more rows
Select columns
stocks %>% select(high, low, volume)
## # A tibble: 5,286 × 3
## high low volume
## <dbl> <dbl> <dbl>
## 1 31 30.7 760600
## 2 30.9 30.7 517100
## 3 30.4 30.1 433100
## 4 29.9 29.4 650600
## 5 30.0 28.8 862500
## 6 29.4 29.0 943600
## 7 30.1 29.5 897900
## 8 30.3 29.6 680200
## 9 30.1 29.4 705900
## 10 29.3 28.8 572900
## # … with 5,276 more rows
Add columns
stocks %>% select(symbol,date, high)
## # A tibble: 5,286 × 3
## symbol date high
## <chr> <date> <dbl>
## 1 HMC 2016-01-04 31
## 2 HMC 2016-01-05 30.9
## 3 HMC 2016-01-06 30.4
## 4 HMC 2016-01-07 29.9
## 5 HMC 2016-01-08 30.0
## 6 HMC 2016-01-11 29.4
## 7 HMC 2016-01-12 30.1
## 8 HMC 2016-01-13 30.3
## 9 HMC 2016-01-14 30.1
## 10 HMC 2016-01-15 29.3
## # … with 5,276 more rows
Summarise with groups
stocks %>% group_by(symbol) %>% summarise(avg_high = mean(high))
## # A tibble: 3 × 2
## symbol avg_high
## <chr> <dbl>
## 1 HMC 28.4
## 2 TGT 121.
## 3 WMT 109.