Import stock prices

stocks <- tq_get(c("NKE", "TSLA"),
                 get = "stock.prices",
                 from = "2019-01-01",
                 to = "2023-01-01")
stocks
## # A tibble: 2,016 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 NKE    2019-01-02  72.8  74.6  72.2  74.1  6762700     70.6
##  2 NKE    2019-01-03  73.2  73.3  71.2  72.8  8007400     69.4
##  3 NKE    2019-01-04  73.4  75.1  73.1  74.7  7844200     71.2
##  4 NKE    2019-01-07  74.7  76.4  74.3  75.7  8184800     72.2
##  5 NKE    2019-01-08  76.8  77.4  76.2  76.7  8809000     73.2
##  6 NKE    2019-01-09  77.0  77.2  76.1  76.6  8591000     73.0
##  7 NKE    2019-01-10  75.6  77.3  75.5  76.4 11148600     72.9
##  8 NKE    2019-01-11  76.3  76.9  75.8  76.0 10689900     72.5
##  9 NKE    2019-01-14  75.5  76.8  75.5  76.1  5677400     72.5
## 10 NKE    2019-01-15  76.2  78.0  76.1  77.9  6212400     74.3
## # ℹ 2,006 more rows

Filter rows

filter(stocks, date == 1, open == 1)
## # A tibble: 0 × 8
## # ℹ 8 variables: symbol <chr>, date <date>, open <dbl>, high <dbl>, low <dbl>,
## #   close <dbl>, volume <dbl>, adjusted <dbl>

Arrange rows

arrange(stocks, desc(date), desc(open))
## # A tibble: 2,016 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 TSLA   2022-12-30  120.  124.  120.  123. 157777300     123.
##  2 NKE    2022-12-30  117.  117.  116.  117.   4355500     116.
##  3 TSLA   2022-12-29  120.  124.  118.  122. 221923300     122.
##  4 NKE    2022-12-29  116.  118.  116.  117.   4588600     116.
##  5 NKE    2022-12-28  118.  118.  115.  115.   5437800     114.
##  6 TSLA   2022-12-28  110.  116.  108.  113. 221070500     113.
##  7 TSLA   2022-12-27  118.  120.  109.  109. 208643400     109.
##  8 NKE    2022-12-27  116.  118.  116.  118.   6661100     116.
##  9 TSLA   2022-12-23  126.  129.  121.  123. 166989700     123.
## 10 NKE    2022-12-23  116.  117.  116.  116.   6603700     115.
## # ℹ 2,006 more rows

Select columns

select(stocks, date:close)
## # A tibble: 2,016 × 5
##    date        open  high   low close
##    <date>     <dbl> <dbl> <dbl> <dbl>
##  1 2019-01-02  72.8  74.6  72.2  74.1
##  2 2019-01-03  73.2  73.3  71.2  72.8
##  3 2019-01-04  73.4  75.1  73.1  74.7
##  4 2019-01-07  74.7  76.4  74.3  75.7
##  5 2019-01-08  76.8  77.4  76.2  76.7
##  6 2019-01-09  77.0  77.2  76.1  76.6
##  7 2019-01-10  75.6  77.3  75.5  76.4
##  8 2019-01-11  76.3  76.9  75.8  76.0
##  9 2019-01-14  75.5  76.8  75.5  76.1
## 10 2019-01-15  76.2  78.0  76.1  77.9
## # ℹ 2,006 more rows
select(stocks, date, open, high, low)
## # A tibble: 2,016 × 4
##    date        open  high   low
##    <date>     <dbl> <dbl> <dbl>
##  1 2019-01-02  72.8  74.6  72.2
##  2 2019-01-03  73.2  73.3  71.2
##  3 2019-01-04  73.4  75.1  73.1
##  4 2019-01-07  74.7  76.4  74.3
##  5 2019-01-08  76.8  77.4  76.2
##  6 2019-01-09  77.0  77.2  76.1
##  7 2019-01-10  75.6  77.3  75.5
##  8 2019-01-11  76.3  76.9  75.8
##  9 2019-01-14  75.5  76.8  75.5
## 10 2019-01-15  76.2  78.0  76.1
## # ℹ 2,006 more rows
select(stocks, date, open, high, low, close)
## # A tibble: 2,016 × 5
##    date        open  high   low close
##    <date>     <dbl> <dbl> <dbl> <dbl>
##  1 2019-01-02  72.8  74.6  72.2  74.1
##  2 2019-01-03  73.2  73.3  71.2  72.8
##  3 2019-01-04  73.4  75.1  73.1  74.7
##  4 2019-01-07  74.7  76.4  74.3  75.7
##  5 2019-01-08  76.8  77.4  76.2  76.7
##  6 2019-01-09  77.0  77.2  76.1  76.6
##  7 2019-01-10  75.6  77.3  75.5  76.4
##  8 2019-01-11  76.3  76.9  75.8  76.0
##  9 2019-01-14  75.5  76.8  75.5  76.1
## 10 2019-01-15  76.2  78.0  76.1  77.9
## # ℹ 2,006 more rows
select(stocks, date, open, close, starts_with("dep"))
## # A tibble: 2,016 × 3
##    date        open close
##    <date>     <dbl> <dbl>
##  1 2019-01-02  72.8  74.1
##  2 2019-01-03  73.2  72.8
##  3 2019-01-04  73.4  74.7
##  4 2019-01-07  74.7  75.7
##  5 2019-01-08  76.8  76.7
##  6 2019-01-09  77.0  76.6
##  7 2019-01-10  75.6  76.4
##  8 2019-01-11  76.3  76.0
##  9 2019-01-14  75.5  76.1
## 10 2019-01-15  76.2  77.9
## # ℹ 2,006 more rows

Add columns

mutate(stocks,
       high = date - volume) %>%
    
    # Select
    select(symbol:date, high)
## # A tibble: 2,016 × 3
##    symbol date       high        
##    <chr>  <date>     <date>      
##  1 NKE    2019-01-02 -16497-05-13
##  2 NKE    2019-01-03 -19905-06-28
##  3 NKE    2019-01-04 -19458-04-27
##  4 NKE    2019-01-07 -20391-10-18
##  5 NKE    2019-01-08 -22100-10-19
##  6 NKE    2019-01-09 -21503-08-30
##  7 NKE    2019-01-10 -28505-03-13
##  8 NKE    2019-01-11 -27249-01-29
##  9 NKE    2019-01-14 -13526-11-04
## 10 NKE    2019-01-15 -14990-01-25
## # ℹ 2,006 more rows
# Just keep high
mutate(stocks,
       high = date - low) %>%
    
    # Select
    select(low)
## # A tibble: 2,016 × 1
##      low
##    <dbl>
##  1  72.2
##  2  71.2
##  3  73.1
##  4  74.3
##  5  76.2
##  6  76.1
##  7  75.5
##  8  75.8
##  9  75.5
## 10  76.1
## # ℹ 2,006 more rows
# alternative using transmute()
transmute(stocks,
          high = date - low)
## # A tibble: 2,016 × 1
##    high      
##    <date>    
##  1 2018-10-21
##  2 2018-10-23
##  3 2018-10-22
##  4 2018-10-24
##  5 2018-10-23
##  6 2018-10-24
##  7 2018-10-26
##  8 2018-10-27
##  9 2018-10-30
## 10 2018-10-30
## # ℹ 2,006 more rows

Summarize by groups

Collapsing data to a single row

stocks
## # A tibble: 2,016 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 NKE    2019-01-02  72.8  74.6  72.2  74.1  6762700     70.6
##  2 NKE    2019-01-03  73.2  73.3  71.2  72.8  8007400     69.4
##  3 NKE    2019-01-04  73.4  75.1  73.1  74.7  7844200     71.2
##  4 NKE    2019-01-07  74.7  76.4  74.3  75.7  8184800     72.2
##  5 NKE    2019-01-08  76.8  77.4  76.2  76.7  8809000     73.2
##  6 NKE    2019-01-09  77.0  77.2  76.1  76.6  8591000     73.0
##  7 NKE    2019-01-10  75.6  77.3  75.5  76.4 11148600     72.9
##  8 NKE    2019-01-11  76.3  76.9  75.8  76.0 10689900     72.5
##  9 NKE    2019-01-14  75.5  76.8  75.5  76.1  5677400     72.5
## 10 NKE    2019-01-15  76.2  78.0  76.1  77.9  6212400     74.3
## # ℹ 2,006 more rows
# average stock price
summarise(stocks, date = mean(high, na.rm = TRUE))
## # A tibble: 1 × 1
##    date
##   <dbl>
## 1  140.

Summarize by group

stocks %>%
    
    # Group by stock
    group_by(symbol) %>%
    
    # Calculate average high
    summarise(high = mean(date, na.rm = TRUE)) %>%
    
    # Sort it
    arrange(high)
## # A tibble: 2 × 2
##   symbol high      
##   <chr>  <date>    
## 1 NKE    2020-12-31
## 2 TSLA   2020-12-31

grouping the mean high and 52 week low

stocks %>%
    group_by(low) %>%
    summarise(count = n(),
              l = mean(low, na.rm = TRUE),
              hi = mean(high, na.rm = TRUE)) %>%
    
    # Plot
    ggplot(mapping = aes(x = l, y = hi)) +
    geom_point(aes(size = count), alpha = 0.3) +
    geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

Missing values

stocks %>%
    
    # Remove missing values
    filter(!is.na(high))
## # A tibble: 2,016 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 NKE    2019-01-02  72.8  74.6  72.2  74.1  6762700     70.6
##  2 NKE    2019-01-03  73.2  73.3  71.2  72.8  8007400     69.4
##  3 NKE    2019-01-04  73.4  75.1  73.1  74.7  7844200     71.2
##  4 NKE    2019-01-07  74.7  76.4  74.3  75.7  8184800     72.2
##  5 NKE    2019-01-08  76.8  77.4  76.2  76.7  8809000     73.2
##  6 NKE    2019-01-09  77.0  77.2  76.1  76.6  8591000     73.0
##  7 NKE    2019-01-10  75.6  77.3  75.5  76.4 11148600     72.9
##  8 NKE    2019-01-11  76.3  76.9  75.8  76.0 10689900     72.5
##  9 NKE    2019-01-14  75.5  76.8  75.5  76.1  5677400     72.5
## 10 NKE    2019-01-15  76.2  78.0  76.1  77.9  6212400     74.3
## # ℹ 2,006 more rows

useful summary functions

grouping multiple variables

stocks %>%
    group_by(date, high, low) %>%
    summarise(count = n()) %>%
    ungroup()
## `summarise()` has grouped output by 'date', 'high'. You can override using the
## `.groups` argument.
## # A tibble: 2,016 × 4
##    date        high   low count
##    <date>     <dbl> <dbl> <int>
##  1 2019-01-02  21.0  19.9     1
##  2 2019-01-02  74.6  72.2     1
##  3 2019-01-03  20.6  19.8     1
##  4 2019-01-03  73.3  71.2     1
##  5 2019-01-04  21.2  20.2     1
##  6 2019-01-04  75.1  73.1     1
##  7 2019-01-07  22.4  21.2     1
##  8 2019-01-07  76.4  74.3     1
##  9 2019-01-08  22.9  21.8     1
## 10 2019-01-08  77.4  76.2     1
## # ℹ 2,006 more rows

ungrouping