stocks <- tq_get(c("PPTA", "IBM"),
get = "stock.prices",
from = "2022-01-01",
to = "2024-01-01")
stocks
## # A tibble: 1,002 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 PPTA 2022-01-03 4.64 4.80 4.61 4.77 80500 4.77
## 2 PPTA 2022-01-04 4.86 4.90 4.70 4.88 92500 4.88
## 3 PPTA 2022-01-05 4.85 4.89 4.5 4.55 92200 4.55
## 4 PPTA 2022-01-06 4.40 4.57 4.38 4.48 76400 4.48
## 5 PPTA 2022-01-07 4.56 4.59 4.36 4.54 53800 4.54
## 6 PPTA 2022-01-10 4.44 4.45 4.14 4.29 96700 4.29
## 7 PPTA 2022-01-11 4.24 4.37 4.12 4.35 72800 4.35
## 8 PPTA 2022-01-12 4.43 4.48 4.27 4.30 68700 4.30
## 9 PPTA 2022-01-13 4.35 4.35 4.20 4.22 54900 4.22
## 10 PPTA 2022-01-14 4.27 4.27 4 4.17 125200 4.17
## # ℹ 992 more rows
stocks %>%
ggplot(aes(x = date, y = adjusted, color = symbol)) +
geom_line()
################################################ #Apply the dplyr verbs
you learned in chapter 5
################################################
stocks %>% filter (adjusted > 4)
## # A tibble: 602 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 PPTA 2022-01-03 4.64 4.80 4.61 4.77 80500 4.77
## 2 PPTA 2022-01-04 4.86 4.90 4.70 4.88 92500 4.88
## 3 PPTA 2022-01-05 4.85 4.89 4.5 4.55 92200 4.55
## 4 PPTA 2022-01-06 4.40 4.57 4.38 4.48 76400 4.48
## 5 PPTA 2022-01-07 4.56 4.59 4.36 4.54 53800 4.54
## 6 PPTA 2022-01-10 4.44 4.45 4.14 4.29 96700 4.29
## 7 PPTA 2022-01-11 4.24 4.37 4.12 4.35 72800 4.35
## 8 PPTA 2022-01-12 4.43 4.48 4.27 4.30 68700 4.30
## 9 PPTA 2022-01-13 4.35 4.35 4.20 4.22 54900 4.22
## 10 PPTA 2022-01-14 4.27 4.27 4 4.17 125200 4.17
## # ℹ 592 more rows
arrange(stocks, desc(close))
## # A tibble: 1,002 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 IBM 2023-12-12 163. 166. 163. 165. 5292300 156.
## 2 IBM 2023-12-28 164. 164. 163. 164. 2071300 155.
## 3 IBM 2023-12-13 164. 165. 163. 164. 4989100 155.
## 4 IBM 2023-12-29 164. 164. 163. 164. 2526200 155.
## 5 IBM 2023-12-11 163. 164. 162. 164. 6077200 155.
## 6 IBM 2023-12-27 163. 164. 163. 163. 3234600 155.
## 7 IBM 2023-12-26 162. 163. 162. 163. 1772400 154.
## 8 IBM 2023-12-14 163. 164. 160. 163. 6129800 154.
## 9 IBM 2023-12-18 162. 163. 162. 163. 3677300 154.
## 10 IBM 2023-12-15 162. 164. 162. 162. 11016100 153.
## # ℹ 992 more rows
select(stocks, date, open, high, close)
## # A tibble: 1,002 × 4
## date open high close
## <date> <dbl> <dbl> <dbl>
## 1 2022-01-03 4.64 4.80 4.77
## 2 2022-01-04 4.86 4.90 4.88
## 3 2022-01-05 4.85 4.89 4.55
## 4 2022-01-06 4.40 4.57 4.48
## 5 2022-01-07 4.56 4.59 4.54
## 6 2022-01-10 4.44 4.45 4.29
## 7 2022-01-11 4.24 4.37 4.35
## 8 2022-01-12 4.43 4.48 4.30
## 9 2022-01-13 4.35 4.35 4.22
## 10 2022-01-14 4.27 4.27 4.17
## # ℹ 992 more rows
select(stocks, date, volume, low)
## # A tibble: 1,002 × 3
## date volume low
## <date> <dbl> <dbl>
## 1 2022-01-03 80500 4.61
## 2 2022-01-04 92500 4.70
## 3 2022-01-05 92200 4.5
## 4 2022-01-06 76400 4.38
## 5 2022-01-07 53800 4.36
## 6 2022-01-10 96700 4.14
## 7 2022-01-11 72800 4.12
## 8 2022-01-12 68700 4.27
## 9 2022-01-13 54900 4.20
## 10 2022-01-14 125200 4
## # ℹ 992 more rows
select(stocks, adjusted, symbol, high, open)
## # A tibble: 1,002 × 4
## adjusted symbol high open
## <dbl> <chr> <dbl> <dbl>
## 1 4.77 PPTA 4.80 4.64
## 2 4.88 PPTA 4.90 4.86
## 3 4.55 PPTA 4.89 4.85
## 4 4.48 PPTA 4.57 4.40
## 5 4.54 PPTA 4.59 4.56
## 6 4.29 PPTA 4.45 4.44
## 7 4.35 PPTA 4.37 4.24
## 8 4.30 PPTA 4.48 4.43
## 9 4.22 PPTA 4.35 4.35
## 10 4.17 PPTA 4.27 4.27
## # ℹ 992 more rows
mutate(stocks,
gain = high - low) %>%
select(gain)
## # A tibble: 1,002 × 1
## gain
## <dbl>
## 1 0.190
## 2 0.200
## 3 0.390
## 4 0.195
## 5 0.230
## 6 0.311
## 7 0.245
## 8 0.212
## 9 0.155
## 10 0.270
## # ℹ 992 more rows
stocks %>%
#Group by Volume
group_by(symbol) %>%
summarise( min_volume = min(volume),
max_volume = max(volume)
)
## # A tibble: 2 × 3
## symbol min_volume max_volume
## <chr> <dbl> <dbl>
## 1 IBM 1477100 37400200
## 2 PPTA 14800 5774800