Import stock prices

stocks <- tq_get(c("AAPL", "ROKU", "CL=F"),
                 get = "stock.prices",
                 from = "2019-01-01",
                 to = "2020-01-01")
stocks
## # A tibble: 756 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   2019-01-02  38.7  39.7  38.6  39.5 148158800     37.9
##  2 AAPL   2019-01-03  36.0  36.4  35.5  35.5 365248800     34.2
##  3 AAPL   2019-01-04  36.1  37.1  36.0  37.1 234428400     35.6
##  4 AAPL   2019-01-07  37.2  37.2  36.5  37.0 219111200     35.5
##  5 AAPL   2019-01-08  37.4  38.0  37.1  37.7 164101200     36.2
##  6 AAPL   2019-01-09  37.8  38.6  37.4  38.3 180396400     36.8
##  7 AAPL   2019-01-10  38.1  38.5  37.7  38.5 143122800     37.0
##  8 AAPL   2019-01-11  38.2  38.4  37.9  38.1 108092800     36.6
##  9 AAPL   2019-01-14  37.7  37.8  37.3  37.5 129756800     36.0
## 10 AAPL   2019-01-15  37.6  38.3  37.5  38.3 114843600     36.8
## # ℹ 746 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(adjusted > 36)
## # A tibble: 750 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   2019-01-02  38.7  39.7  38.6  39.5 148158800     37.9
##  2 AAPL   2019-01-08  37.4  38.0  37.1  37.7 164101200     36.2
##  3 AAPL   2019-01-09  37.8  38.6  37.4  38.3 180396400     36.8
##  4 AAPL   2019-01-10  38.1  38.5  37.7  38.5 143122800     37.0
##  5 AAPL   2019-01-11  38.2  38.4  37.9  38.1 108092800     36.6
##  6 AAPL   2019-01-14  37.7  37.8  37.3  37.5 129756800     36.0
##  7 AAPL   2019-01-15  37.6  38.3  37.5  38.3 114843600     36.8
##  8 AAPL   2019-01-16  38.3  39.0  38.2  38.7 122278800     37.2
##  9 AAPL   2019-01-17  38.5  39.4  38.3  39.0 119284800     37.4
## 10 AAPL   2019-01-18  39.4  39.5  39.0  39.2 135004000     37.7
## # ℹ 740 more rows

Arrange rows

arrange(stocks, desc(date), desc(high))
## # A tibble: 756 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 ROKU   2019-12-31 129.  135.  127.  134.   13763900    134. 
##  2 AAPL   2019-12-31  72.5  73.4  72.4  73.4 100805600     71.6
##  3 CL=F   2019-12-31  61.7  61.9  60.6  61.1    494535     61.1
##  4 ROKU   2019-12-30 138.  139.  131.  132.   14469100    132. 
##  5 AAPL   2019-12-30  72.4  73.2  71.3  72.9 144114400     71.1
##  6 CL=F   2019-12-30  61.7  62.3  61.1  61.7    427149     61.7
##  7 ROKU   2019-12-27 145.  145.  139   140.   13303200    140. 
##  8 AAPL   2019-12-27  72.8  73.5  72.0  72.4 146266000     70.7
##  9 CL=F   2019-12-27  61.7  62.0  61.2  61.7    351899     61.7
## 10 ROKU   2019-12-26 147.  149.  144.  145.   14660200    145. 
## # ℹ 746 more rows

Select columns

select(stocks, date, symbol, adjusted)
## # A tibble: 756 × 3
##    date       symbol adjusted
##    <date>     <chr>     <dbl>
##  1 2019-01-02 AAPL       37.9
##  2 2019-01-03 AAPL       34.2
##  3 2019-01-04 AAPL       35.6
##  4 2019-01-07 AAPL       35.5
##  5 2019-01-08 AAPL       36.2
##  6 2019-01-09 AAPL       36.8
##  7 2019-01-10 AAPL       37.0
##  8 2019-01-11 AAPL       36.6
##  9 2019-01-14 AAPL       36.0
## 10 2019-01-15 AAPL       36.8
## # ℹ 746 more rows

Add columns

mutate(stocks,
       daily_returns = (adjusted - lag(adjusted))/lag(adjusted)) %>%
    
    # Select symbol, date, daily_returns
    select(symbol, date, daily_returns)
## # A tibble: 756 × 3
##    symbol date       daily_returns
##    <chr>  <date>             <dbl>
##  1 AAPL   2019-01-02      NA      
##  2 AAPL   2019-01-03      -0.0996 
##  3 AAPL   2019-01-04       0.0427 
##  4 AAPL   2019-01-07      -0.00223
##  5 AAPL   2019-01-08       0.0191 
##  6 AAPL   2019-01-09       0.0170 
##  7 AAPL   2019-01-10       0.00320
##  8 AAPL   2019-01-11      -0.00982
##  9 AAPL   2019-01-14      -0.0150 
## 10 AAPL   2019-01-15       0.0205 
## # ℹ 746 more rows

Summarise with groups

stocks %>%
    group_by(symbol) %>%
    summarise(count = n(), avg_monthly_return = mean(adjusted - lag(adjusted))/lag(adjusted), na.rm = TRUE)
## Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
## dplyr 1.1.0.
## ℹ Please use `reframe()` instead.
## ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
##   always returns an ungrouped data frame and adjust accordingly.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `summarise()` has grouped output by 'symbol'. You can override using the
## `.groups` argument.
## # A tibble: 756 × 4
## # Groups:   symbol [3]
##    symbol count avg_monthly_return na.rm
##    <chr>  <int>              <dbl> <lgl>
##  1 AAPL     252                 NA TRUE 
##  2 AAPL     252                 NA TRUE 
##  3 AAPL     252                 NA TRUE 
##  4 AAPL     252                 NA TRUE 
##  5 AAPL     252                 NA TRUE 
##  6 AAPL     252                 NA TRUE 
##  7 AAPL     252                 NA TRUE 
##  8 AAPL     252                 NA TRUE 
##  9 AAPL     252                 NA TRUE 
## 10 AAPL     252                 NA TRUE 
## # ℹ 746 more rows