# Load libraries
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.1 ✔ stringr 1.5.2
## ✔ ggplot2 4.0.0 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
# Import data
stock_df <- read_csv("stock_df.csv")
## Rows: 5 Columns: 106
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): company
## dbl (105): 2019_week1, 2019_week2, 2019_week3, 2019_week4, 2019_week5, 2019_...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Reshape from wide to long
stock_df_long <- stock_df %>%
pivot_longer(
cols = starts_with("2019_week"),
names_to = "week",
values_to = "price"
) %>%
separate(week, into = c("year", "week"), sep = "_week") %>%
mutate(
year = as.integer(year),
week = as.integer(week)
)
# View result
head(stock_df_long)
## # A tibble: 6 × 57
## company `2020_week1` `2020_week2` `2020_week3` `2020_week4` `2020_week5`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Amazon 1875. 1883. 1865. 1862. 2009.
## 2 Amazon 1875. 1883. 1865. 1862. 2009.
## 3 Amazon 1875. 1883. 1865. 1862. 2009.
## 4 Amazon 1875. 1883. 1865. 1862. 2009.
## 5 Amazon 1875. 1883. 1865. 1862. 2009.
## 6 Amazon 1875. 1883. 1865. 1862. 2009.
## # ℹ 51 more variables: `2020_week6` <dbl>, `2020_week7` <dbl>,
## # `2020_week8` <dbl>, `2020_week9` <dbl>, `2020_week10` <dbl>,
## # `2020_week11` <dbl>, `2020_week12` <dbl>, `2020_week13` <dbl>,
## # `2020_week14` <dbl>, `2020_week15` <dbl>, `2020_week16` <dbl>,
## # `2020_week17` <dbl>, `2020_week18` <dbl>, `2020_week19` <dbl>,
## # `2020_week20` <dbl>, `2020_week21` <dbl>, `2020_week22` <dbl>,
## # `2020_week23` <dbl>, `2020_week24` <dbl>, `2020_week25` <dbl>, …
# Reshape from wide to long (all years)
stock_df_long <- stock_df %>%
pivot_longer(
cols = matches("^\\d{4}_week\\d+$"), # matches 2019_week1, 2020_week2, etc.
names_to = "week",
values_to = "price"
) %>%
separate(week, into = c("year", "week"), sep = "_week") %>%
mutate(
year = as.integer(year),
week = as.integer(week)
)
# Check result
head(stock_df_long)
## # A tibble: 6 × 4
## company year week price
## <chr> <int> <int> <dbl>
## 1 Amazon 2019 1 1848.
## 2 Amazon 2019 2 1641.
## 3 Amazon 2019 3 1696.
## 4 Amazon 2019 4 1671.
## 5 Amazon 2019 5 1626.
## 6 Amazon 2019 6 1588.