Import stock prices

stocks <- tq_get(c("HMC", "TM"),
                 get = "stock.prices",
                 from = "2016-01-01",
                 to = "2024-01-01")
stocks
## # A tibble: 4,024 × 8
##    symbol date        open  high   low close volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl>
##  1 HMC    2016-01-04  30.9  31    30.7  31.0 760600     25.1
##  2 HMC    2016-01-05  30.8  30.9  30.7  30.8 517100     24.9
##  3 HMC    2016-01-06  30.4  30.4  30.1  30.2 433100     24.5
##  4 HMC    2016-01-07  29.8  29.9  29.4  29.5 650600     23.9
##  5 HMC    2016-01-08  29.9  30.0  28.8  28.9 862500     23.4
##  6 HMC    2016-01-11  29.1  29.4  29.0  29.4 943600     23.8
##  7 HMC    2016-01-12  30.1  30.1  29.5  29.9 897900     24.2
##  8 HMC    2016-01-13  30.3  30.3  29.6  29.7 680200     24.1
##  9 HMC    2016-01-14  29.7  30.1  29.4  30.0 705900     24.3
## 10 HMC    2016-01-15  29.1  29.3  28.8  29.0 572900     23.5
## # ℹ 4,014 more rows

Plot stock prices

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

#Apply #############################

Filter rows

stocks %>% filter(adjusted > 30)
## # A tibble: 2,174 × 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 HMC    2018-01-08  35.6  35.8  35.6  35.8  287100     30.1
##  2 HMC    2018-01-10  36.3  36.5  36.2  36.3  713700     30.5
##  3 HMC    2018-01-11  35.9  36.1  35.8  36.1  987600     30.4
##  4 HMC    2018-01-12  35.8  35.9  35.7  35.9 2198700     30.2
##  5 HMC    2018-01-16  36.0  36.1  35.7  35.7  514400     30.1
##  6 HMC    2018-01-17  36.1  36.5  36.1  36.3  563300     30.6
##  7 HMC    2018-01-18  35.9  35.9  35.7  35.9  303500     30.2
##  8 HMC    2018-01-19  36.1  36.3  36.1  36.2  372400     30.5
##  9 HMC    2018-01-22  36    36.2  35.9  36.2  456100     30.5
## 10 HMC    2018-01-23  36.4  36.5  36.4  36.5  376500     30.7
## # ℹ 2,164 more rows

arange rows

arrange(stocks, desc(open), desc(close))
## # A tibble: 4,024 × 8
##    symbol date        open  high   low close volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl>
##  1 TM     2022-01-13  211   214.  210.  211. 547000     211.
##  2 TM     2022-01-14  211.  211.  209.  211. 267700     211.
##  3 TM     2022-01-18  209.  209.  207.  207. 379600     207.
##  4 TM     2022-01-19  207.  207.  202.  202. 562200     202.
##  5 TM     2022-01-12  205   208.  205.  207. 511400     207.
##  6 TM     2022-01-20  204.  205.  201.  201. 283000     201.
##  7 TM     2022-02-09  202.  204.  200.  201. 342500     201.
##  8 TM     2022-01-11  201.  202.  200.  202. 255500     202.
##  9 TM     2022-01-10  201.  201.  198.  200. 380000     200.
## 10 TM     2022-01-07  201.  202.  199.  200. 371000     200.
## # ℹ 4,014 more rows

select rows

select(stocks, open, high, volume, adjusted)
## # A tibble: 4,024 × 4
##     open  high volume adjusted
##    <dbl> <dbl>  <dbl>    <dbl>
##  1  30.9  31   760600     25.1
##  2  30.8  30.9 517100     24.9
##  3  30.4  30.4 433100     24.5
##  4  29.8  29.9 650600     23.9
##  5  29.9  30.0 862500     23.4
##  6  29.1  29.4 943600     23.8
##  7  30.1  30.1 897900     24.2
##  8  30.3  30.3 680200     24.1
##  9  29.7  30.1 705900     24.3
## 10  29.1  29.3 572900     23.5
## # ℹ 4,014 more rows

add columns

mutate(stocks,
       gain = open - close) %>%
    select(high:low, gain)
## # A tibble: 4,024 × 3
##     high   low    gain
##    <dbl> <dbl>   <dbl>
##  1  31    30.7 -0.130 
##  2  30.9  30.7  0.0500
##  3  30.4  30.1  0.140 
##  4  29.9  29.4  0.25  
##  5  30.0  28.8  1.05  
##  6  29.4  29.0 -0.210 
##  7  30.1  29.5  0.190 
##  8  30.3  29.6  0.560 
##  9  30.1  29.4 -0.300 
## 10  29.3  28.8  0.110 
## # ℹ 4,014 more rows

##summarise

summarise(stocks, mean(close, na.rm = TRUE))
## # A tibble: 1 × 1
##   `mean(close, na.rm = TRUE)`
##                         <dbl>
## 1                        83.1