Import stock prices
stocks <- tq_get(c("NDA-FI.HE", "JPM", "DNB.OL", "MS"),
get = "stock.prices",
from = "2016-01-01")
stocks
## # A tibble: 8,745 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NDA-FI.HE 2016-01-04 10.2 10.3 10.1 10.1 703596 5.51
## 2 NDA-FI.HE 2016-01-05 10.1 10.1 10.1 10.1 0 5.51
## 3 NDA-FI.HE 2016-01-07 9.95 9.95 9.79 9.88 906429 5.37
## 4 NDA-FI.HE 2016-01-08 9.88 9.88 9.88 9.88 0 5.37
## 5 NDA-FI.HE 2016-01-11 9.60 9.71 9.57 9.64 1030007 5.24
## 6 NDA-FI.HE 2016-01-12 9.62 9.81 9.57 9.72 1365547 5.28
## 7 NDA-FI.HE 2016-01-13 9.82 9.96 9.77 9.82 1997363 5.34
## 8 NDA-FI.HE 2016-01-14 9.78 9.78 9.53 9.64 2605240 5.24
## 9 NDA-FI.HE 2016-01-15 9.63 9.65 9.28 9.31 2522004 5.06
## 10 NDA-FI.HE 2016-01-18 9.34 9.41 9.21 9.23 1406527 5.02
## # ℹ 8,735 more rows
Apply the dplyr verbs you learned in chapter 5
Filter rows
stocks %>% filter(adjusted > 100)
## # A tibble: 2,777 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 JPM 2019-07-23 115. 116. 115. 116. 12752200 101.
## 2 JPM 2019-07-24 116. 117. 116. 117. 10011000 101.
## 3 JPM 2019-07-26 116. 117. 116. 116. 12800400 100.
## 4 JPM 2019-07-29 116. 117. 116. 116. 8500400 100.
## 5 JPM 2019-07-31 116. 116. 115. 116 13648000 100.
## 6 JPM 2019-09-10 116. 117. 116. 117. 14333100 101.
## 7 JPM 2019-09-11 117. 117. 115. 117. 9919200 101.
## 8 JPM 2019-09-12 117. 118. 116. 118. 12283500 102.
## 9 JPM 2019-09-13 119 120. 119. 120. 16257200 104.
## 10 JPM 2019-09-16 119. 120. 118. 119. 11993900 103.
## # ℹ 2,767 more rows
Arrange rows
arrange(stocks, desc(symbol), desc(date))
## # A tibble: 8,745 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NDA-FI.HE 2024-09-10 10.6 10.6 10.4 10.4 3592004 10.4
## 2 NDA-FI.HE 2024-09-09 10.5 10.6 10.4 10.6 6708356 10.6
## 3 NDA-FI.HE 2024-09-06 10.7 10.7 10.4 10.4 4955551 10.4
## 4 NDA-FI.HE 2024-09-05 10.5 10.8 10.5 10.7 4800436 10.7
## 5 NDA-FI.HE 2024-09-04 10.5 10.6 10.4 10.5 4386458 10.5
## 6 NDA-FI.HE 2024-09-03 10.7 10.7 10.6 10.6 3784591 10.6
## 7 NDA-FI.HE 2024-09-02 10.7 10.8 10.7 10.7 2785763 10.7
## 8 NDA-FI.HE 2024-08-30 10.6 10.7 10.6 10.7 5678522 10.7
## 9 NDA-FI.HE 2024-08-29 10.6 10.7 10.6 10.6 3185130 10.6
## 10 NDA-FI.HE 2024-08-28 10.6 10.6 10.6 10.6 3370665 10.6
## # ℹ 8,735 more rows
Select columns
select(stocks, symbol:volume)
## # A tibble: 8,745 × 7
## symbol date open high low close volume
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NDA-FI.HE 2016-01-04 10.2 10.3 10.1 10.1 703596
## 2 NDA-FI.HE 2016-01-05 10.1 10.1 10.1 10.1 0
## 3 NDA-FI.HE 2016-01-07 9.95 9.95 9.79 9.88 906429
## 4 NDA-FI.HE 2016-01-08 9.88 9.88 9.88 9.88 0
## 5 NDA-FI.HE 2016-01-11 9.60 9.71 9.57 9.64 1030007
## 6 NDA-FI.HE 2016-01-12 9.62 9.81 9.57 9.72 1365547
## 7 NDA-FI.HE 2016-01-13 9.82 9.96 9.77 9.82 1997363
## 8 NDA-FI.HE 2016-01-14 9.78 9.78 9.53 9.64 2605240
## 9 NDA-FI.HE 2016-01-15 9.63 9.65 9.28 9.31 2522004
## 10 NDA-FI.HE 2016-01-18 9.34 9.41 9.21 9.23 1406527
## # ℹ 8,735 more rows
select(stocks, symbol, date, open, close, volume)
## # A tibble: 8,745 × 5
## symbol date open close volume
## <chr> <date> <dbl> <dbl> <dbl>
## 1 NDA-FI.HE 2016-01-04 10.2 10.1 703596
## 2 NDA-FI.HE 2016-01-05 10.1 10.1 0
## 3 NDA-FI.HE 2016-01-07 9.95 9.88 906429
## 4 NDA-FI.HE 2016-01-08 9.88 9.88 0
## 5 NDA-FI.HE 2016-01-11 9.60 9.64 1030007
## 6 NDA-FI.HE 2016-01-12 9.62 9.72 1365547
## 7 NDA-FI.HE 2016-01-13 9.82 9.82 1997363
## 8 NDA-FI.HE 2016-01-14 9.78 9.64 2605240
## 9 NDA-FI.HE 2016-01-15 9.63 9.31 2522004
## 10 NDA-FI.HE 2016-01-18 9.34 9.23 1406527
## # ℹ 8,735 more rows
Add colums
mutate(stocks,
gain = open - close) %>%
# select open, close, and gain
select(symbol: open, close, gain)
## # A tibble: 8,745 × 5
## symbol date open close gain
## <chr> <date> <dbl> <dbl> <dbl>
## 1 NDA-FI.HE 2016-01-04 10.2 10.1 0.100
## 2 NDA-FI.HE 2016-01-05 10.1 10.1 0
## 3 NDA-FI.HE 2016-01-07 9.95 9.88 0.0700
## 4 NDA-FI.HE 2016-01-08 9.88 9.88 0
## 5 NDA-FI.HE 2016-01-11 9.60 9.64 -0.0350
## 6 NDA-FI.HE 2016-01-12 9.62 9.72 -0.0950
## 7 NDA-FI.HE 2016-01-13 9.82 9.82 0
## 8 NDA-FI.HE 2016-01-14 9.78 9.64 0.145
## 9 NDA-FI.HE 2016-01-15 9.63 9.31 0.315
## 10 NDA-FI.HE 2016-01-18 9.34 9.23 0.110
## # ℹ 8,735 more rows
mutate(stocks,
gain = open - close) %>%
# just collect gain
select(gain)
## # A tibble: 8,745 × 1
## gain
## <dbl>
## 1 0.100
## 2 0
## 3 0.0700
## 4 0
## 5 -0.0350
## 6 -0.0950
## 7 0
## 8 0.145
## 9 0.315
## 10 0.110
## # ℹ 8,735 more rows
select(stocks, adjusted) %>%
mutate(adjusted_lag1 = lag(adjusted))
## # A tibble: 8,745 × 2
## adjusted adjusted_lag1
## <dbl> <dbl>
## 1 5.51 NA
## 2 5.51 5.51
## 3 5.37 5.51
## 4 5.37 5.37
## 5 5.24 5.37
## 6 5.28 5.24
## 7 5.34 5.28
## 8 5.24 5.34
## 9 5.06 5.24
## 10 5.02 5.06
## # ℹ 8,735 more rows
Summarize with groups
stocks %>%
group_by(symbol) %>%
summarise(mean_adjusted = mean(adjusted, na.rm = TRUE))
## # A tibble: 4 × 2
## symbol mean_adjusted
## <chr> <dbl>
## 1 DNB.OL 129.
## 2 JPM 109.
## 3 MS 56.4
## 4 NDA-FI.HE 6.78
stocks %>%
# Group by symbol
group_by(symbol) %>%
summarise(
mean_open = mean(open, na.rm = TRUE),
mean_high = mean(high, na.rm = TRUE),
mean_low = mean(low, na.rm = TRUE),
mean_close = mean(close, na.rm = TRUE))
## # A tibble: 4 × 5
## symbol mean_open mean_high mean_low mean_close
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 DNB.OL 165. 166. 163. 165.
## 2 JPM 121. 122. 120. 121.
## 3 MS 63.1 63.8 62.4 63.1
## 4 NDA-FI.HE 9.07 9.15 8.99 9.07