unlink(“~/Library/Caches/R/sass”, recursive = TRUE) unlink(tempdir(), recursive = TRUE) install.packages(“sass”, type = “binary”) xcode-select –install library(tidyverse) library(dplyr) — title: “Stock Data Transformation with pivot_longer()” author: “Your Name” date: “2025-10-21” output: html_document —
This document demonstrates how to transform wide-format stock data
into long format using pivot_longer()
from the tidyverse
package.
library(tidyverse)
Before transformation, the data looks like this (wide format):
## # A tibble: 3 × 5
## company `2020_week1` `2020_week2` `2021_week1` `2021_week2`
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Apple 120 125 130 135
## 2 Google 1500 1520 1550 1580
## 3 Microsoft 180 185 190 195
stock_long <- stock_df %>%
pivot_longer(
cols = !company,
names_to = c("year", "week"),
names_sep = "_week",
names_transform = list(year = as.integer, week = as.integer),
values_to = "price"
)
stock_long
## # A tibble: 12 × 4
## company year week price
## <chr> <int> <int> <dbl>
## 1 Apple 2020 1 120
## 2 Apple 2020 2 125
## 3 Apple 2021 1 130
## 4 Apple 2021 2 135
## 5 Google 2020 1 1500
## 6 Google 2020 2 1520
## 7 Google 2021 1 1550
## 8 Google 2021 2 1580
## 9 Microsoft 2020 1 180
## 10 Microsoft 2020 2 185
## 11 Microsoft 2021 1 190
## 12 Microsoft 2021 2 195
cols = !company
: Select all columns
except ‘company’ to pivotnames_to = c("year", "week")
: Split
column names into two new columnsnames_sep = "_week"
: Use “_week” as
the delimiter to split namesnames_transform
: Convert year and week
to integersvalues_to = "price"
: Store the cell
values in a column called ‘price’# Check the structure
glimpse(stock_long)
## Rows: 12
## Columns: 4
## $ company <chr> "Apple", "Apple", "Apple", "Apple", "Google", "Google", "Googl…
## $ year <int> 2020, 2020, 2021, 2021, 2020, 2020, 2021, 2021, 2020, 2020, 20…
## $ week <int> 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2
## $ price <dbl> 120, 125, 130, 135, 1500, 1520, 1550, 1580, 180, 185, 190, 195
# Summary statistics
stock_long %>%
group_by(company, year) %>%
summarise(
avg_price = mean(price),
min_price = min(price),
max_price = max(price),
.groups = "drop"
)
## # A tibble: 6 × 5
## company year avg_price min_price max_price
## <chr> <int> <dbl> <dbl> <dbl>
## 1 Apple 2020 122. 120 125
## 2 Apple 2021 132. 130 135
## 3 Google 2020 1510 1500 1520
## 4 Google 2021 1565 1550 1580
## 5 Microsoft 2020 182. 180 185
## 6 Microsoft 2021 192. 190 195