Import stock prices

stocks <- tq_get(c("CAT", "NVDA", "TSM"),
                 get = "stock.prices",
                 from = "2016-01-01")
stocks
## # A tibble: 8,070 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 CAT    2016-01-04  66.9  68.1  65.7  68.0  8586900     52.9
##  2 CAT    2016-01-05  68.4  68.4  66.4  67.3  6139100     52.3
##  3 CAT    2016-01-06  66.0  66.8  65.6  66.2  6639300     51.5
##  4 CAT    2016-01-07  65.1  65.5  63.7  63.9  8601500     49.7
##  5 CAT    2016-01-08  64.3  64.5  62.9  63.3  8278500     49.2
##  6 CAT    2016-01-11  63.7  63.7  60.8  61.5  9669400     47.8
##  7 CAT    2016-01-12  62.4  62.5  60.4  61.6  8593100     47.9
##  8 CAT    2016-01-13  62.1  62.8  60.4  60.9  7237300     47.4
##  9 CAT    2016-01-14  61.2  63.0  60.4  62.3  9241800     48.4
## 10 CAT    2016-01-15  59.5  60.1  58.8  59.9 12674500     47.2
## # ℹ 8,060 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(open > 85, close > 90 )
## # A tibble: 4,318 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 CAT    2016-11-09  89.8  93.2  89.4  91.2 19281900     73.9
##  2 CAT    2016-11-10  92.0  94.9  91.8  93.4 14174400     75.7
##  3 CAT    2016-11-11  93.4  94.3  91.8  93.0  7369900     75.3
##  4 CAT    2016-11-14  93.2  95.5  93.0  94.2  7552800     76.3
##  5 CAT    2016-11-15  93.5  94.5  92.6  94.4  4645400     76.5
##  6 CAT    2016-11-16  93.8  94.4  92.8  93.3  3297200     75.6
##  7 CAT    2016-11-17  93.5  93.5  92.4  92.8  3942200     75.2
##  8 CAT    2016-11-18  93.0  93.4  91.9  92.3  4715900     74.8
##  9 CAT    2016-11-21  93.1  93.8  92.8  92.9  3783200     75.3
## 10 CAT    2016-11-22  93.4  93.7  92.8  93.6  3172800     75.8
## # ℹ 4,308 more rows

Arrange Rows

stocks %>% arrange(desc(high), desc(low))
## # A tibble: 8,070 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 CAT    2026-06-30 1045  1073. 1037. 1065.  3287500    1063.
##  2 CAT    2026-06-25 1025. 1057. 1015  1057.  4642100    1055.
##  3 CAT    2026-07-01 1034. 1041.  985.  991.  3914100     990.
##  4 CAT    2026-06-29  998. 1039.  992. 1033.  3146900    1031.
##  5 CAT    2026-06-26 1032. 1032.  990.  997. 13948500     996.
##  6 CAT    2026-06-22  999. 1023.  999  1022.  4033500    1020.
##  7 CAT    2026-06-24  982. 1005.  970.  994.  3526000     993.
##  8 CAT    2026-07-02 1001. 1001.  949.  964.  3568600     962.
##  9 CAT    2026-06-23  985.  995.  973.  984.  4286900     982.
## 10 CAT    2026-06-18  979   994.  976.  986.  6175000     984.
## # ℹ 8,060 more rows

Select Columns

 select(stocks, symbol, open:close)
## # A tibble: 8,070 × 5
##    symbol  open  high   low close
##    <chr>  <dbl> <dbl> <dbl> <dbl>
##  1 CAT     66.9  68.1  65.7  68.0
##  2 CAT     68.4  68.4  66.4  67.3
##  3 CAT     66.0  66.8  65.6  66.2
##  4 CAT     65.1  65.5  63.7  63.9
##  5 CAT     64.3  64.5  62.9  63.3
##  6 CAT     63.7  63.7  60.8  61.5
##  7 CAT     62.4  62.5  60.4  61.6
##  8 CAT     62.1  62.8  60.4  60.9
##  9 CAT     61.2  63.0  60.4  62.3
## 10 CAT     59.5  60.1  58.8  59.9
## # ℹ 8,060 more rows

Add Columns

stocks %>% mutate(gain = close - open) %>% select(symbol, open:close, gain)
## # A tibble: 8,070 × 6
##    symbol  open  high   low close   gain
##    <chr>  <dbl> <dbl> <dbl> <dbl>  <dbl>
##  1 CAT     66.9  68.1  65.7  68.0  1.11 
##  2 CAT     68.4  68.4  66.4  67.3 -1.10 
##  3 CAT     66.0  66.8  65.6  66.2  0.240
##  4 CAT     65.1  65.5  63.7  63.9 -1.14 
##  5 CAT     64.3  64.5  62.9  63.3 -1.04 
##  6 CAT     63.7  63.7  60.8  61.5 -2.20 
##  7 CAT     62.4  62.5  60.4  61.6 -0.840
##  8 CAT     62.1  62.8  60.4  60.9 -1.18 
##  9 CAT     61.2  63.0  60.4  62.3  1.03 
## 10 CAT     59.5  60.1  58.8  59.9  0.350
## # ℹ 8,060 more rows
transmute(stocks, gain = close - open)
## # A tibble: 8,070 × 1
##      gain
##     <dbl>
##  1  1.11 
##  2 -1.10 
##  3  0.240
##  4 -1.14 
##  5 -1.04 
##  6 -2.20 
##  7 -0.840
##  8 -1.18 
##  9  1.03 
## 10  0.350
## # ℹ 8,060 more rows

Summarise with groups

stocks %>% summarise(avg_open = mean(open))
## # A tibble: 1 × 1
##   avg_open
##      <dbl>
## 1     134.
stocks %>% 
    #Grouo by ticker symbol
    group_by(symbol) %>%
    # Calculate average open price
    summarise(avg_open = mean(open)) %>%
    # Sort it
    arrange(avg_open)
## # A tibble: 3 × 2
##   symbol avg_open
##   <chr>     <dbl>
## 1 NVDA       46.8
## 2 TSM       110. 
## 3 CAT       244.