Import stock prices

stocks <- tq_get(c("BALY", "GE", "NVDA", "SNAP", "CRON", "XOM"),
                 get = "stock.prices",
                 from = "2018-01-01",
                 to = "2024-06-01")
stocks
## # A tibble: 9,335 × 8
##    symbol date        open  high   low close volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl>
##  1 BALY   2019-03-29  29    30.0  29    30.0  61200     29.6
##  2 BALY   2019-04-01  31    31.1  30.5  31.0 222600     30.6
##  3 BALY   2019-04-02  31    31.5  30.6  31    40900     30.6
##  4 BALY   2019-04-03  31    31    29.9  31    28400     30.6
##  5 BALY   2019-04-04  31.1  31.2  31.0  31   369400     30.6
##  6 BALY   2019-04-05  31.2  31.5  30.9  31.5 179900     31.1
##  7 BALY   2019-04-08  31.1  31.8  31.1  31.7 535500     31.3
##  8 BALY   2019-04-09  32    32    31.0  31.7 310100     31.3
##  9 BALY   2019-04-10  32.0  33.4  31.5  32.2 324300     31.8
## 10 BALY   2019-04-11  32.7  33.2  32.7  33   474200     32.6
## # ℹ 9,325 more rows

Plot stock prices

stocks %>%
    
    ggplot(aes(x = date, y = adjusted, color = symbol)) +
    geom_line()

Apply the dplyr Verbs you learned in Ch 5

Filter Rows

stocks %>% filter(adjusted > 100)
## # A tibble: 439 × 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 GE     2023-12-19  98.7  101.  98.7  101. 5957138     100.
##  2 GE     2023-12-21  99.1  101.  99.1  101. 5185165     101.
##  3 GE     2023-12-22 102.   102. 101.   101. 4030650     101.
##  4 GE     2023-12-26 101.   102. 101.   102. 2480063     101.
##  5 GE     2023-12-27 101.   102. 101.   102. 3314310     102.
##  6 GE     2023-12-28 102.   103. 102.   102. 3965494     102.
##  7 GE     2023-12-29 102.   103. 101.   102. 4441133     102.
##  8 GE     2024-01-02 101.   102. 100.   101. 5189425     100.
##  9 GE     2024-01-05  99.7  101.  99.5  101. 4190157     100.
## 10 GE     2024-01-08 101.   102.  99.1  102. 6920068     102.
## # ℹ 429 more rows

Arrange Rows

arrange(stocks, desc(close))
## # A tibble: 9,335 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 GE     2024-05-08  168.  170.  168.  169.  4878200     169.
##  2 GE     2024-05-07  168.  171.  167.  169.  4781800     168.
##  3 GE     2024-05-28  167   169.  165.  169.  5749700     168.
##  4 GE     2024-05-06  165.  168.  165.  168.  4831600     168.
##  5 GE     2024-05-09  169.  169.  167.  168.  4968800     167.
##  6 GE     2024-05-24  166.  169.  164.  167.  5498700     167.
##  7 GE     2024-05-23  162.  166.  162.  165.  7411500     165.
##  8 GE     2024-05-31  164.  165.  160.  165. 11704800     165.
##  9 GE     2024-04-29  163   166.  163   164.  5371700     164.
## 10 GE     2024-05-30  163.  165.  163.  164.  4385400     164.
## # ℹ 9,325 more rows

Select Columns

select(stocks, date, open, high, close)
## # A tibble: 9,335 × 4
##    date        open  high close
##    <date>     <dbl> <dbl> <dbl>
##  1 2019-03-29  29    30.0  30.0
##  2 2019-04-01  31    31.1  31.0
##  3 2019-04-02  31    31.5  31  
##  4 2019-04-03  31    31    31  
##  5 2019-04-04  31.1  31.2  31  
##  6 2019-04-05  31.2  31.5  31.5
##  7 2019-04-08  31.1  31.8  31.7
##  8 2019-04-09  32    32    31.7
##  9 2019-04-10  32.0  33.4  32.2
## 10 2019-04-11  32.7  33.2  33  
## # ℹ 9,325 more rows
select(stocks, date, volume, low)
## # A tibble: 9,335 × 3
##    date       volume   low
##    <date>      <dbl> <dbl>
##  1 2019-03-29  61200  29  
##  2 2019-04-01 222600  30.5
##  3 2019-04-02  40900  30.6
##  4 2019-04-03  28400  29.9
##  5 2019-04-04 369400  31.0
##  6 2019-04-05 179900  30.9
##  7 2019-04-08 535500  31.1
##  8 2019-04-09 310100  31.0
##  9 2019-04-10 324300  31.5
## 10 2019-04-11 474200  32.7
## # ℹ 9,325 more rows
select(stocks, adjusted, symbol, high, open)
## # A tibble: 9,335 × 4
##    adjusted symbol  high  open
##       <dbl> <chr>  <dbl> <dbl>
##  1     29.6 BALY    30.0  29  
##  2     30.6 BALY    31.1  31  
##  3     30.6 BALY    31.5  31  
##  4     30.6 BALY    31    31  
##  5     30.6 BALY    31.2  31.1
##  6     31.1 BALY    31.5  31.2
##  7     31.3 BALY    31.8  31.1
##  8     31.3 BALY    32    32  
##  9     31.8 BALY    33.4  32.0
## 10     32.6 BALY    33.2  32.7
## # ℹ 9,325 more rows

Summarize by Groups

stocks %>%
    
    #Group by Volume
    group_by(symbol) %>%
    summarise( min_volume =  min(volume),
             max_volume = max(volume)
             )
## # A tibble: 6 × 3
##   symbol min_volume max_volume
##   <chr>       <dbl>      <dbl>
## 1 BALY        28400    5640000
## 2 CRON       482100  129502100
## 3 GE        1981995   80465058
## 4 NVDA     97884000 2511528000
## 5 SNAP      5271200  330993900
## 6 XOM       3979400   84439400

Add New Columns

mutate(stocks,
       gain = high - low) %>%
    select(gain)
## # A tibble: 9,335 × 1
##     gain
##    <dbl>
##  1 0.950
##  2 0.586
##  3 0.890
##  4 1.10 
##  5 0.260
##  6 0.640
##  7 0.730
##  8 0.980
##  9 1.89 
## 10 0.525
## # ℹ 9,325 more rows