Import stock prices

stocks <- tq_get(c("LULU", "CLX", "MMC"),
                 get = "stock.prices",
                 from = "2019-06-01",
                 to = "2020-06-01")
stocks
## # A tibble: 753 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 LULU   2019-06-03  166.  168.  162.  164.  1634900     164.
##  2 LULU   2019-06-04  165.  169.  165.  169   1273600     169 
##  3 LULU   2019-06-05  170.  174.  169.  174.  1814500     174.
##  4 LULU   2019-06-06  174.  174.  170.  170.  1685700     170.
##  5 LULU   2019-06-07  172.  173.  170.  172.  1846600     172.
##  6 LULU   2019-06-10  174.  176.  171.  171.  1930900     171.
##  7 LULU   2019-06-11  172.  174.  170.  172.  2016200     172.
##  8 LULU   2019-06-12  172.  173.  169.  171.  5417600     171.
##  9 LULU   2019-06-13  182.  183.  172.  175. 11076700     175.
## 10 LULU   2019-06-14  173.  178.  172.  177.  3635700     177.
## # … with 743 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 > 150)
## # A tibble: 339 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 LULU   2019-06-03  166.  168.  162.  164.  1634900     164.
##  2 LULU   2019-06-04  165.  169.  165.  169   1273600     169 
##  3 LULU   2019-06-05  170.  174.  169.  174.  1814500     174.
##  4 LULU   2019-06-06  174.  174.  170.  170.  1685700     170.
##  5 LULU   2019-06-07  172.  173.  170.  172.  1846600     172.
##  6 LULU   2019-06-10  174.  176.  171.  171.  1930900     171.
##  7 LULU   2019-06-11  172.  174.  170.  172.  2016200     172.
##  8 LULU   2019-06-12  172.  173.  169.  171.  5417600     171.
##  9 LULU   2019-06-13  182.  183.  172.  175. 11076700     175.
## 10 LULU   2019-06-14  173.  178.  172.  177.  3635700     177.
## # … with 329 more rows

Arrange Rows

arrange(stocks, desc(open), desc(volume))
## # A tibble: 753 × 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 LULU   2020-05-29  286.  301.  284.  300. 4871000     300.
##  2 LULU   2020-05-26  283.  285.  279.  281. 2118500     281.
##  3 LULU   2020-05-28  281.  291.  278.  284. 2307900     284.
##  4 LULU   2020-05-27  281.  283.  272.  282. 2158700     282.
##  5 LULU   2020-05-22  269.  277.  267.  277. 2073900     277.
##  6 LULU   2020-02-21  263   265.  254.  256. 1587700     256.
##  7 LULU   2020-02-20  263.  266.  259.  264. 1518400     264.
##  8 LULU   2020-02-19  261   264.  260.  263.  908000     263.
##  9 LULU   2020-05-21  259.  268.  258.  268. 1808400     268.
## 10 LULU   2020-05-19  257.  257.  251.  251. 1480500     251.
## # … with 743 more rows

Select columns

select(stocks, symbol:close)
## # A tibble: 753 × 6
##    symbol date        open  high   low close
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>
##  1 LULU   2019-06-03  166.  168.  162.  164.
##  2 LULU   2019-06-04  165.  169.  165.  169 
##  3 LULU   2019-06-05  170.  174.  169.  174.
##  4 LULU   2019-06-06  174.  174.  170.  170.
##  5 LULU   2019-06-07  172.  173.  170.  172.
##  6 LULU   2019-06-10  174.  176.  171.  171.
##  7 LULU   2019-06-11  172.  174.  170.  172.
##  8 LULU   2019-06-12  172.  173.  169.  171.
##  9 LULU   2019-06-13  182.  183.  172.  175.
## 10 LULU   2019-06-14  173.  178.  172.  177.
## # … with 743 more rows

Add Columns

mutate(stocks,
       gain = close - open) %>%
       select(symbol:close, gain)    
## # A tibble: 753 × 7
##    symbol date        open  high   low close   gain
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>
##  1 LULU   2019-06-03  166.  168.  162.  164. -1.96 
##  2 LULU   2019-06-04  165.  169.  165.  169   3.60 
##  3 LULU   2019-06-05  170.  174.  169.  174.  3.38 
##  4 LULU   2019-06-06  174.  174.  170.  170. -3.13 
##  5 LULU   2019-06-07  172.  173.  170.  172.  0.830
##  6 LULU   2019-06-10  174.  176.  171.  171. -2.99 
##  7 LULU   2019-06-11  172.  174.  170.  172. -0.680
##  8 LULU   2019-06-12  172.  173.  169.  171. -1.04 
##  9 LULU   2019-06-13  182.  183.  172.  175. -7.47 
## 10 LULU   2019-06-14  173.  178.  172.  177.  3.67 
## # … with 743 more rows

Summarize with groups

stocks
## # A tibble: 753 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 LULU   2019-06-03  166.  168.  162.  164.  1634900     164.
##  2 LULU   2019-06-04  165.  169.  165.  169   1273600     169 
##  3 LULU   2019-06-05  170.  174.  169.  174.  1814500     174.
##  4 LULU   2019-06-06  174.  174.  170.  170.  1685700     170.
##  5 LULU   2019-06-07  172.  173.  170.  172.  1846600     172.
##  6 LULU   2019-06-10  174.  176.  171.  171.  1930900     171.
##  7 LULU   2019-06-11  172.  174.  170.  172.  2016200     172.
##  8 LULU   2019-06-12  172.  173.  169.  171.  5417600     171.
##  9 LULU   2019-06-13  182.  183.  172.  175. 11076700     175.
## 10 LULU   2019-06-14  173.  178.  172.  177.  3635700     177.
## # … with 743 more rows
summarise(stocks, Avgopen = mean(open,
na.rm = TRUE))
## # A tibble: 1 × 1
##   Avgopen
##     <dbl>
## 1    158.
stocks %>%
    
    
    group_by(symbol) %>%
    
    
    summarise(Avgopen = mean(open, na.rm = TRUE)) %>%
    
   
    arrange(Avgopen)
## # A tibble: 3 × 2
##   symbol Avgopen
##   <chr>    <dbl>
## 1 MMC       102.
## 2 CLX       163.
## 3 LULU      208.