Import your data

# Excel File
data <- read_excel("../00_data/myData.xlsx")

Pivoting

Data_Small <- data %>%
    select(stock_symbol, high, low) %>%
    filter(stock_symbol %in% c("INTC", "NVDA"))

long to wide form

Data_Small %>% pivot_wider(names_from = stock_symbol, values_from =low )
## Warning: Values from `low` are not uniquely identified; output will contain list-cols.
## • Use `values_fn = list` to suppress this warning.
## • Use `values_fn = {summary_fun}` to summarise duplicates.
## • Use the following dplyr code to identify duplicates.
##   {data} |>
##   dplyr::summarise(n = dplyr::n(), .by = c(high, stock_symbol)) |>
##   dplyr::filter(n > 1L)
## # A tibble: 4,648 × 3
##     high INTC      NVDA  
##    <dbl> <list>    <list>
##  1  21.0 <dbl [3]> <NULL>
##  2  21.0 <dbl [3]> <NULL>
##  3  20.9 <dbl [4]> <NULL>
##  4  20.8 <dbl [3]> <NULL>
##  5  20.9 <dbl [1]> <NULL>
##  6  21.2 <dbl [1]> <NULL>
##  7  20.9 <dbl [2]> <NULL>
##  8  21.1 <dbl [3]> <NULL>
##  9  21.5 <dbl [3]> <NULL>
## 10  21.4 <dbl [3]> <NULL>
## # ℹ 4,638 more rows
Data_Small %>% slice(-4638)
## # A tibble: 6,541 × 3
##    stock_symbol  high   low
##    <chr>        <dbl> <dbl>
##  1 INTC          21.0  20.7
##  2 INTC          21.0  20.6
##  3 INTC          20.9  20.7
##  4 INTC          20.8  20.3
##  5 INTC          20.9  20.4
##  6 INTC          21.2  20.8
##  7 INTC          20.9  20.4
##  8 INTC          21.1  20.4
##  9 INTC          21.5  21.0
## 10 INTC          21.4  20.8
## # ℹ 6,531 more rows

wide to long form

Separating and Uniting

Uniting two columns

data_united <- data %>%
        
    unite(col = "High and Low", high:low, sep = "/", remove = FALSE)

Separate a column

data_united %>%
    
    separate(col = `High and Low`, into = c("high","low"), sep = "/")
## # A tibble: 45,088 × 8
##    stock_symbol date                 open high     low    close adj_close volume
##    <chr>        <dttm>              <dbl> <chr>    <chr>  <dbl>     <dbl>  <dbl>
##  1 AAPL         2010-01-04 00:00:00  7.62 7.660714 7.585   7.64      6.52 4.94e8
##  2 AAPL         2010-01-05 00:00:00  7.66 7.699643 7.616…  7.66      6.53 6.02e8
##  3 AAPL         2010-01-06 00:00:00  7.66 7.686786 7.526…  7.53      6.42 5.52e8
##  4 AAPL         2010-01-07 00:00:00  7.56 7.571429 7.466…  7.52      6.41 4.77e8
##  5 AAPL         2010-01-08 00:00:00  7.51 7.571429 7.466…  7.57      6.45 4.48e8
##  6 AAPL         2010-01-11 00:00:00  7.6  7.607143 7.444…  7.50      6.40 4.62e8
##  7 AAPL         2010-01-12 00:00:00  7.47 7.491786 7.372…  7.42      6.32 5.94e8
##  8 AAPL         2010-01-13 00:00:00  7.42 7.533214 7.289…  7.52      6.41 6.06e8
##  9 AAPL         2010-01-14 00:00:00  7.50 7.516429 7.465   7.48      6.38 4.33e8
## 10 AAPL         2010-01-15 00:00:00  7.53 7.557143 7.3525  7.35      6.27 5.94e8
## # ℹ 45,078 more rows

Missing Values