# Load necessary libraries
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(tidyr)
library(readr)

# Read the dataset (update the file path as needed)
stock_df <- read_csv("stock_df (1).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.
# Transform the dataset into a long format
stock_df_long <- stock_df %>%
  pivot_longer(
    cols = -company, # Exclude the 'company' column
    names_to = c("year", "week"), # Split column names into 'year' and 'week'
    names_sep = "_week", # Separator to split by
    names_transform = list(year = as.integer, week = as.integer), # Convert year and week to integers
    values_to = "price" # Column to store the values
  )

# View the transformed dataset
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.
# Save the output if needed
write_csv(stock_df_long, "stock_df_long.csv")