Import stock prices

stocks <- tq_get(c("SPOT", "ISRG", "KHC", "FIS", "GOOG"),
                 get = "stock.prices",
                 from = "2020-01-01",)
stocks
## # A tibble: 5,900 × 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 SPOT   2020-01-02  151   153.  150.  152.  662600     152.
##  2 SPOT   2020-01-03  150.  154.  150.  152. 1018400     152.
##  3 SPOT   2020-01-06  151.  157   150.  157. 1311900     157.
##  4 SPOT   2020-01-07  157.  158.  155.  156.  876700     156.
##  5 SPOT   2020-01-08  156.  159.  155.  159.  974500     159.
##  6 SPOT   2020-01-09  158.  160.  157.  158. 1630600     158.
##  7 SPOT   2020-01-10  158.  159.  155.  156. 1046400     156.
##  8 SPOT   2020-01-13  152.  154.  150.  151. 1880900     151.
##  9 SPOT   2020-01-14  152.  152.  148.  149. 1048000     149.
## 10 SPOT   2020-01-15  149.  153.  149.  152. 1037400     152.
## # ℹ 5,890 more rows

Plot stock prices

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

Apply the dplyr Verbs from Chapter 5

Filter Rows

stocks %>% filter(adjusted > 24)
## # A tibble: 5,837 × 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 SPOT   2020-01-02  151   153.  150.  152.  662600     152.
##  2 SPOT   2020-01-03  150.  154.  150.  152. 1018400     152.
##  3 SPOT   2020-01-06  151.  157   150.  157. 1311900     157.
##  4 SPOT   2020-01-07  157.  158.  155.  156.  876700     156.
##  5 SPOT   2020-01-08  156.  159.  155.  159.  974500     159.
##  6 SPOT   2020-01-09  158.  160.  157.  158. 1630600     158.
##  7 SPOT   2020-01-10  158.  159.  155.  156. 1046400     156.
##  8 SPOT   2020-01-13  152.  154.  150.  151. 1880900     151.
##  9 SPOT   2020-01-14  152.  152.  148.  149. 1048000     149.
## 10 SPOT   2020-01-15  149.  153.  149.  152. 1037400     152.
## # ℹ 5,827 more rows

Arrange Rows

arrange(stocks, desc(date))
## # A tibble: 5,900 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 SPOT   2024-09-10 327.  328.  324.  327     741400    327  
##  2 ISRG   2024-09-10 478.  485.  478.  484.   1006500    484. 
##  3 KHC    2024-09-10  36.1  36.2  35.4  35.5  4669300     35.5
##  4 FIS    2024-09-10  82.2  83.5  81.1  83.4  2980600     83.4
##  5 GOOG   2024-09-10 151.  152.  150.  150.  20401800    150. 
##  6 SPOT   2024-09-09 324.  329.  322.  324.    828000    324. 
##  7 ISRG   2024-09-09 475   480.  474.  479.   1121500    479. 
##  8 KHC    2024-09-09  35.9  36.2  35.7  36.0  6559000     36.0
##  9 FIS    2024-09-09  81.9  83.0  81.3  82.4  3340400     82.0
## 10 GOOG   2024-09-09 154.  155.  148.  150.  28057700    150. 
## # ℹ 5,890 more rows

Select Columns

select(stocks, symbol:close, adjusted)
## # A tibble: 5,900 × 7
##    symbol date        open  high   low close adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>
##  1 SPOT   2020-01-02  151   153.  150.  152.     152.
##  2 SPOT   2020-01-03  150.  154.  150.  152.     152.
##  3 SPOT   2020-01-06  151.  157   150.  157.     157.
##  4 SPOT   2020-01-07  157.  158.  155.  156.     156.
##  5 SPOT   2020-01-08  156.  159.  155.  159.     159.
##  6 SPOT   2020-01-09  158.  160.  157.  158.     158.
##  7 SPOT   2020-01-10  158.  159.  155.  156.     156.
##  8 SPOT   2020-01-13  152.  154.  150.  151.     151.
##  9 SPOT   2020-01-14  152.  152.  148.  149.     149.
## 10 SPOT   2020-01-15  149.  153.  149.  152.     152.
## # ℹ 5,890 more rows

Add columns

// I tried making gain/loss per month, it does per day. But it works I think so..

mutate(stocks,
       year = year(date),
       month = month(date)
       ) %>%
select(symbol, year, month, everything(), -date) %>%
    group_by(symbol, year, month) %>%
    mutate( gain_loss = adjusted - lag(adjusted,)) %>%
    ungroup() 
## # A tibble: 5,900 × 10
##    symbol  year month  open  high   low close  volume adjusted gain_loss
##    <chr>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>     <dbl>
##  1 SPOT    2020     1  151   153.  150.  152.  662600     152.    NA    
##  2 SPOT    2020     1  150.  154.  150.  152. 1018400     152.     0.880
##  3 SPOT    2020     1  151.  157   150.  157. 1311900     157.     4.22 
##  4 SPOT    2020     1  157.  158.  155.  156.  876700     156.    -0.700
##  5 SPOT    2020     1  156.  159.  155.  159.  974500     159.     2.76 
##  6 SPOT    2020     1  158.  160.  157.  158. 1630600     158.    -1.04 
##  7 SPOT    2020     1  158.  159.  155.  156. 1046400     156.    -1.70 
##  8 SPOT    2020     1  152.  154.  150.  151. 1880900     151.    -5.17 
##  9 SPOT    2020     1  152.  152.  148.  149. 1048000     149.    -1.47 
## 10 SPOT    2020     1  149.  153.  149.  152. 1037400     152.     3.05 
## # ℹ 5,890 more rows

Summarize With Groups

# creates year and month, groups by symbol,year,month, calculates avg_adj/month
mutate(stocks, 
       year = year(date), 
         month = month(date)) %>%  
  group_by(symbol, year, month) %>%  
  summarize(avg_adjusted = mean(adjusted, na.rm = TRUE)) %>%
    ungroup()
## `summarise()` has grouped output by 'symbol', 'year'. You can override using
## the `.groups` argument.
## # A tibble: 285 × 4
##    symbol  year month avg_adjusted
##    <chr>  <dbl> <dbl>        <dbl>
##  1 FIS     2020     1         131.
##  2 FIS     2020     2         136.
##  3 FIS     2020     3         114.
##  4 FIS     2020     4         113.
##  5 FIS     2020     5         121.
##  6 FIS     2020     6         126.
##  7 FIS     2020     7         128.
##  8 FIS     2020     8         133.
##  9 FIS     2020     9         135.
## 10 FIS     2020    10         130.
## # ℹ 275 more rows