I have a dataset in R that looks like this:
data = structure(list(quarter = c("Q1 2005", "Q2 2005", "Q3 2005", "Q4 2005",
"Q1 2006"), value = c(128.76, 178.83, 140.9, 188.3, 194.05)), class = "data.frame", row.names = c(NA,
-5L))
I want to expand this dataset to have an extra column called "month" (e.g. jan, feb, mar....) ... and I want to split the difference across all months between successive quarters such that the numbers still add up between quarters.
I tried to do it like this:
library(dplyr)
expand_dataset <- function(data) {
quarter_to_months <- list(
"Q1" = c("Jan", "Feb", "Mar"),
"Q2" = c("Apr", "May", "Jun"),
"Q3" = c("Jul", "Aug", "Sep"),
"Q4" = c("Oct", "Nov", "Dec")
)
expanded_data <- data.frame()
for (i in 1:(nrow(data) - 1)) {
current_value <- data$value[i]
next_value <- data$value[i + 1]
diff <- (next_value - current_value) / 3
quarter <- substr(data$quarter[i], 1, 2)
year <- substr(data$quarter[i], 4, 7)
months <- quarter_to_months[[quarter]]
for (j in 1:3) {
month_value <- current_value + (j - 1) * diff
expanded_data <- rbind(expanded_data, data.frame(
quarter = data$quarter[i],
month = months[j],
year = year,
value = month_value
))
}
}
return(expanded_data)
}
expanded_data <- expand_dataset(data)
print(expanded_data)
Is this the correct way to do this? Is there an easier way to do this?