Import stock prices

stocks <- tq_get(c("AAPL", "NVDA"),
                 get = "stock.prices",
                 from = "2023-01-01",
                 to = "2024-01-01")
stocks
## # A tibble: 500 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   2023-01-03  130.  131.  124.  125. 112117500     123.
##  2 AAPL   2023-01-04  127.  129.  125.  126.  89113600     125.
##  3 AAPL   2023-01-05  127.  128.  125.  125.  80962700     123.
##  4 AAPL   2023-01-06  126.  130.  125.  130.  87754700     128.
##  5 AAPL   2023-01-09  130.  133.  130.  130.  70790800     128.
##  6 AAPL   2023-01-10  130.  131.  128.  131.  63896200     129.
##  7 AAPL   2023-01-11  131.  134.  130.  133.  69458900     132.
##  8 AAPL   2023-01-12  134.  134.  131.  133.  71379600     132.
##  9 AAPL   2023-01-13  132.  135.  132.  135.  57809700     133.
## 10 AAPL   2023-01-17  135.  137.  134.  136.  63646600     134.
## # ℹ 490 more rows

Plot stock prices

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

Filter Rows

stocks %>% filter(adjusted > 24)
## # A tibble: 453 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   2023-01-03  130.  131.  124.  125. 112117500     123.
##  2 AAPL   2023-01-04  127.  129.  125.  126.  89113600     125.
##  3 AAPL   2023-01-05  127.  128.  125.  125.  80962700     123.
##  4 AAPL   2023-01-06  126.  130.  125.  130.  87754700     128.
##  5 AAPL   2023-01-09  130.  133.  130.  130.  70790800     128.
##  6 AAPL   2023-01-10  130.  131.  128.  131.  63896200     129.
##  7 AAPL   2023-01-11  131.  134.  130.  133.  69458900     132.
##  8 AAPL   2023-01-12  134.  134.  131.  133.  71379600     132.
##  9 AAPL   2023-01-13  132.  135.  132.  135.  57809700     133.
## 10 AAPL   2023-01-17  135.  137.  134.  136.  63646600     134.
## # ℹ 443 more rows

Arrange Rows

stocks %>% arrange(open, desc(high), low)
## # A tibble: 500 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 NVDA   2023-01-06  14.5  15.0  14.0  14.9 405044000     14.8
##  2 NVDA   2023-01-05  14.5  14.6  14.1  14.3 389168000     14.3
##  3 NVDA   2023-01-04  14.6  14.9  14.2  14.7 431324000     14.7
##  4 NVDA   2023-01-03  14.9  15.0  14.1  14.3 401277000     14.3
##  5 NVDA   2023-01-09  15.3  16.1  15.1  15.6 504231000     15.6
##  6 NVDA   2023-01-10  15.5  16.0  15.5  15.9 384101000     15.9
##  7 NVDA   2023-01-11  15.8  16.0  15.6  16.0 353285000     16.0
##  8 NVDA   2023-01-12  16.1  16.6  15.5  16.5 551409000     16.5
##  9 NVDA   2023-01-13  16.3  16.9  16.2  16.9 447287000     16.9
## 10 NVDA   2023-01-17  16.9  17.7  16.9  17.7 511102000     17.7
## # ℹ 490 more rows

Select Columns

library(tidyverse)
stocks %>% select (open, high, low, close)
## # A tibble: 500 × 4
##     open  high   low close
##    <dbl> <dbl> <dbl> <dbl>
##  1  130.  131.  124.  125.
##  2  127.  129.  125.  126.
##  3  127.  128.  125.  125.
##  4  126.  130.  125.  130.
##  5  130.  133.  130.  130.
##  6  130.  131.  128.  131.
##  7  131.  134.  130.  133.
##  8  134.  134.  131.  133.
##  9  132.  135.  132.  135.
## 10  135.  137.  134.  136.
## # ℹ 490 more rows

Add Columns

library(tidyverse)
stocks %>% select (open, high, low, close)
## # A tibble: 500 × 4
##     open  high   low close
##    <dbl> <dbl> <dbl> <dbl>
##  1  130.  131.  124.  125.
##  2  127.  129.  125.  126.
##  3  127.  128.  125.  125.
##  4  126.  130.  125.  130.
##  5  130.  133.  130.  130.
##  6  130.  131.  128.  131.
##  7  131.  134.  130.  133.
##  8  134.  134.  131.  133.
##  9  132.  135.  132.  135.
## 10  135.  137.  134.  136.
## # ℹ 490 more rows
mutate(stocks, 
       gain = high - low)
## # A tibble: 500 × 9
##    symbol date        open  high   low close    volume adjusted  gain
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl> <dbl>
##  1 AAPL   2023-01-03  130.  131.  124.  125. 112117500     123.  6.73
##  2 AAPL   2023-01-04  127.  129.  125.  126.  89113600     125.  3.58
##  3 AAPL   2023-01-05  127.  128.  125.  125.  80962700     123.  3.01
##  4 AAPL   2023-01-06  126.  130.  125.  130.  87754700     128.  5.40
##  5 AAPL   2023-01-09  130.  133.  130.  130.  70790800     128.  3.52
##  6 AAPL   2023-01-10  130.  131.  128.  131.  63896200     129.  3.14
##  7 AAPL   2023-01-11  131.  134.  130.  133.  69458900     132.  3.05
##  8 AAPL   2023-01-12  134.  134.  131.  133.  71379600     132.  2.82
##  9 AAPL   2023-01-13  132.  135.  132.  135.  57809700     133.  3.26
## 10 AAPL   2023-01-17  135.  137.  134.  136.  63646600     134.  3.16
## # ℹ 490 more rows

Summarize with groups

library(tidyverse)
stocks%>%
    

    group_by(symbol) %>%
    
    
    summarize(gain = mean(high- low , na.rm = TRUE)) %>%
    

    arrange(gain)
## # A tibble: 2 × 2
##   symbol  gain
##   <chr>  <dbl>
## 1 NVDA    1.20
## 2 AAPL    2.88