Load the necessary packages

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2
## ── 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

Import the data

stock_df <- read.csv("stock_df.csv")

Transform from wide to long format

stock_df_long <- stock_df %>% 
  pivot_longer(cols = -company, 
               names_to = c("year", "week"),
               names_pattern = "(\\d{4})_week(\\d+)",
               values_to = "price")

Display the top rows of the new dataframe

head(stock_df_long)
## # A tibble: 6 × 4
##   company year  week  price
##   <chr>   <chr> <chr> <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.