For this analysis,with the help of llms I will use a dataset containing daily closing prices of multiple cryptocurrencies from January 1, 2022 generated by llm. The goal is to calculate:
We will achieve this using window functions in
dplyr.
library(dplyr)
library(lubridate)
library(tidyr)
# Example: Generate a sample dataset
set.seed(123)
dates <- seq.Date(from = as.Date("2022-01-01"), to = as.Date("2022-12-31"), by = "day")
crypto <- c("Bitcoin", "Ethereum")
data <- expand.grid(date = dates, crypto = crypto)
# Simulate daily closing prices
data$price <- round(runif(nrow(data), 1000, 50000), 2)
# Preview dataset
head(data)
data_analysis <- data %>%
arrange(crypto, date) %>%
group_by(crypto) %>%
mutate(ytd_avg = cummean(price)) %>%
mutate( ma_6day = zoo::rollmean(price, k = 6, fill = NA, align = "right")) %>%
ungroup()
head(data_analysis, 10)
#Conclusion This assignment demonstrates the application of window functions to time series data containing multiple independent items. Using synthetic cryptocurrency data, I computed both the cumulative year-to-date average and a six-day moving average for each asset.
Window functions allow us to perform rolling and cumulative calculations without aggregating or collapsing the dataset, preserving the full time series structure. This approach is particularly useful for financial analysis, forecasting, and exploratory data analysis involving temporal trends.