install.packages("readr")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
install.packages("tidyr")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(readr)
library(tidyr)
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.
stock_df_long <- pivot_longer(stock_df,
cols = -company,
names_to = "week",
values_to = "price",
names_prefix = "2019_week")
stock_df_long$year <- 2019
head(stock_df_long)
## # A tibble: 6 × 4
## company week price year
## <chr> <chr> <dbl> <dbl>
## 1 Amazon 1 1848. 2019
## 2 Amazon 2 1641. 2019
## 3 Amazon 3 1696. 2019
## 4 Amazon 4 1671. 2019
## 5 Amazon 5 1626. 2019
## 6 Amazon 6 1588. 2019