Imported Stock Prices

stocks <- tq_get(c("FNV", "WPM", "NEM", "CCJ", "CEG", "BWXT", "NVDA", "TSM", "ASML", "CAT", "ITW", "HON", "PLD", "EQIX", "PSA", "TPL", "XOM", "CVX"),
                 get = "stock.prices",
                 from = "2024-01-01",
                 to = "2026-09-14")

# Sector Table
sector_map <- tibble::tribble(
    ~symbol, ~sector,
  "FNV",  "Precious Metals",
  "WPM",  "Precious Metals",
  "NEM",  "Precious Metals",
  "CCJ",  "Nuclear/Uranium",
  "CEG",  "Nuclear/Uranium",
  "BWXT", "Nuclear/Uranium",
  "NVDA", "Semiconductors",
  "TSM",  "Semiconductors",
  "ASML", "Semiconductors",
  "CAT",  "Industrials",
  "ITW",  "Industrials",
  "HON",  "Industrials",
  "PLD",  "Real Estate",
  "EQIX", "Real Estate",
  "PSA",  "Real Estate",
  "TPL",  "Energy",
  "XOM",  "Energy",
  "CVX",  "Energy"
)

# Joining "sector" Info Onto "stocks"
stocks <- stocks %>%
    left_join(sector_map, by = "symbol")

stocks
## # A tibble: 12,168 × 9
##    symbol date        open  high   low close  volume adjusted sector         
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl> <chr>          
##  1 FNV    2024-01-02  111.  113.  111.  111.  809400     109. Precious Metals
##  2 FNV    2024-01-03  110.  114.  109.  112. 1339600     110. Precious Metals
##  3 FNV    2024-01-04  112.  112.  111.  111.  528800     109. Precious Metals
##  4 FNV    2024-01-05  111.  112.  109.  110.  658700     108. Precious Metals
##  5 FNV    2024-01-08  109   110.  108.  109.  515600     107. Precious Metals
##  6 FNV    2024-01-09  109.  109.  107.  107.  857100     105. Precious Metals
##  7 FNV    2024-01-10  107.  108   106.  106.  857300     104. Precious Metals
##  8 FNV    2024-01-11  106.  107.  105.  106.  702200     104. Precious Metals
##  9 FNV    2024-01-12  108.  110.  108.  110.  676200     107. Precious Metals
## 10 FNV    2024-01-16  109.  110.  107.  109.  848000     107. Precious Metals
## # ℹ 12,158 more rows

Plotted Stock Prices

p <- stocks %>%
    
    ggplot(aes(x = date, y = adjusted, color = symbol,
            text = paste0(symbol, "<br>", date, "<br>$", round(adjusted, 2)))) +
    geom_line(aes(group = symbol))

ggplotly(p, tooltip = "text")

Applying dplyr Verbs

Filtered Rows

filter(stocks, date == "2026-09-11")
## # A tibble: 18 × 9
##    symbol date         open   high    low  close   volume adjusted sector       
##    <chr>  <date>      <dbl>  <dbl>  <dbl>  <dbl>    <dbl>    <dbl> <chr>        
##  1 FNV    2026-09-11  264    268.   263.   266     734200    266   Precious Met…
##  2 WPM    2026-09-11  154.   156.   153.   154.   1203200    154.  Precious Met…
##  3 NEM    2026-09-11  127.   129.   126.   127.   5554000    127.  Precious Met…
##  4 CCJ    2026-09-11   98.1   98.3   96.4   96.7  2137700     96.7 Nuclear/Uran…
##  5 CEG    2026-09-11  291.   291.   284.   285.   1719500    285.  Nuclear/Uran…
##  6 BWXT   2026-09-11  154.   154.   149.   150.    764300    150.  Nuclear/Uran…
##  7 NVDA   2026-09-11  221.   222    218.   218.  88804100    218.  Semiconducto…
##  8 TSM    2026-09-11  432.   435.   429.   433.  10629200    433.  Semiconducto…
##  9 ASML   2026-09-11 1727.  1727.  1696.  1698.    881200   1698.  Semiconducto…
## 10 CAT    2026-09-11  818.   826    814.   819.   1824600    819.  Industrials  
## 11 ITW    2026-09-11  268.   270.   266.   268.   1019600    268.  Industrials  
## 12 HON    2026-09-11  204.   204.   202.   202.   2746400    202.  Industrials  
## 13 PLD    2026-09-11  136.   136.   135.   136.   2375400    136.  Real Estate  
## 14 EQIX   2026-09-11 1025.  1047.  1024.  1038.    558900   1038.  Real Estate  
## 15 PSA    2026-09-11  297.   298.   293.   296.    683100    296.  Real Estate  
## 16 TPL    2026-09-11  363.   371.   363.   369.    271000    369.  Energy       
## 17 XOM    2026-09-11  165.   167.   164.   166.  11362400    166.  Energy       
## 18 CVX    2026-09-11  212.   216.   212.   214.   6570300    214.  Energy

Arranged Rows

arrange(stocks, desc(date), sector, close)
## # A tibble: 12,168 × 9
##    symbol date        open  high   low close   volume adjusted sector         
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl> <chr>          
##  1 XOM    2026-09-11 165.  167.  164.  166.  11362400    166.  Energy         
##  2 CVX    2026-09-11 212.  216.  212.  214.   6570300    214.  Energy         
##  3 TPL    2026-09-11 363.  371.  363.  369.    271000    369.  Energy         
##  4 HON    2026-09-11 204.  204.  202.  202.   2746400    202.  Industrials    
##  5 ITW    2026-09-11 268.  270.  266.  268.   1019600    268.  Industrials    
##  6 CAT    2026-09-11 818.  826   814.  819.   1824600    819.  Industrials    
##  7 CCJ    2026-09-11  98.1  98.3  96.4  96.7  2137700     96.7 Nuclear/Uranium
##  8 BWXT   2026-09-11 154.  154.  149.  150.    764300    150.  Nuclear/Uranium
##  9 CEG    2026-09-11 291.  291.  284.  285.   1719500    285.  Nuclear/Uranium
## 10 NEM    2026-09-11 127.  129.  126.  127.   5554000    127.  Precious Metals
## # ℹ 12,158 more rows

Select Columns

select(stocks, symbol, open:close)
## # A tibble: 12,168 × 5
##    symbol  open  high   low close
##    <chr>  <dbl> <dbl> <dbl> <dbl>
##  1 FNV     111.  113.  111.  111.
##  2 FNV     110.  114.  109.  112.
##  3 FNV     112.  112.  111.  111.
##  4 FNV     111.  112.  109.  110.
##  5 FNV     109   110.  108.  109.
##  6 FNV     109.  109.  107.  107.
##  7 FNV     107.  108   106.  106.
##  8 FNV     106.  107.  105.  106.
##  9 FNV     108.  110.  108.  110.
## 10 FNV     109.  110.  107.  109.
## # ℹ 12,158 more rows

Add Columns

mutate(stocks,
       days_gain = close - open) %>%
    
    select(symbol, date, sector, days_gain)
## # A tibble: 12,168 × 4
##    symbol date       sector          days_gain
##    <chr>  <date>     <chr>               <dbl>
##  1 FNV    2024-01-02 Precious Metals     0.670
##  2 FNV    2024-01-03 Precious Metals     2.80 
##  3 FNV    2024-01-04 Precious Metals    -0.620
##  4 FNV    2024-01-05 Precious Metals    -1.19 
##  5 FNV    2024-01-08 Precious Metals     0.360
##  6 FNV    2024-01-09 Precious Metals    -2.49 
##  7 FNV    2024-01-10 Precious Metals    -1.04 
##  8 FNV    2024-01-11 Precious Metals    -0.140
##  9 FNV    2024-01-12 Precious Metals     1.59 
## 10 FNV    2024-01-16 Precious Metals     0.800
## # ℹ 12,158 more rows

Summarized with Groups

stocks_gains <- stocks %>%
  group_by(symbol) %>%
  arrange(date, .by_group = TRUE) %>%
  mutate(
    days_gain     = close - open,
    overnight_gain = open - lag(close),
    total_gain     = close - lag(close)) %>%
    
  summarize(
    avg_intraday  = mean(days_gain, na.rm = TRUE),
    avg_overnight = mean(overnight_gain, na.rm = TRUE),
    avg_total     = mean(total_gain, na.rm = TRUE)) %>%
    
  arrange(avg_total)

stocks_gains
## # A tibble: 18 × 4
##    symbol avg_intraday avg_overnight avg_total
##    <chr>         <dbl>         <dbl>     <dbl>
##  1 PSA       -0.0313         0.0188   -0.0227 
##  2 HON       -0.119          0.113    -0.00627
##  3 PLD       -0.000754       0.00507   0.00167
##  4 ITW       -0.0456         0.0577    0.0112 
##  5 CCJ       -0.0526         0.132     0.0809 
##  6 XOM        0.0507         0.0456    0.0943 
##  7 CVX       -0.0308         0.126     0.0957 
##  8 BWXT      -0.189          0.299     0.109  
##  9 NEM        0.107          0.0191    0.127  
## 10 WPM        0.0613         0.0939    0.157  
## 11 FNV        0.0355         0.194     0.229  
## 12 CEG       -0.392          0.642     0.251  
## 13 NVDA      -0.0918         0.342     0.252  
## 14 TPL       -0.0653         0.350     0.283  
## 15 EQIX       0.00129        0.347     0.337  
## 16 TSM       -0.156          0.647     0.491  
## 17 CAT       -0.0232         0.801     0.779  
## 18 ASML      -0.485          1.92      1.45