Other questions on reshape() helped me reformat my data (see below), but I'm wondering if there's a way to achieve what I did in a simpler way (less code) using base R, no packages. I have example data that looks like this:
period store_id product price1 price2 price3 quantity1 quantity2 quantity3
201801 1 1 11 5 6 100 200 300
201802 1 1 12 6 6 100 200 300
201803 1 1 13 7 6 100 200 300
201804 1 1 14 8 6 100 200 300
201805 1 1 15 9 6 100 200 300
201806 2 2 16 10 6 100 200 300
201807 2 2 17 11 6 100 200 300
201808 2 2 18 12 6 100 200 300
201809 2 2 19 13 6 100 200 300
201810 2 2 20 14 6 100 200 300
And I'd like for it to look something like this (every period has 3 rows, one column for price and one column for quantity):
period store_id product quantity type price
201801 1 1 100 price1 11
201801 1 1 200 price2 5
201801 1 1 300 price3 6
201802 1 1 100 price1 12
201802 1 1 200 price2 6
201802 1 1 300 price3 6
201803 1 1 100 price1 13
201803 1 1 200 price2 7
201803 1 1 300 price3 6
201804 1 1 100 price1 14
and so on...
I was able to reformat my data by first splitting the data into two data frames, one for prices and one for quantities, and then merging them back together afterwards. Is there a simpler way to use reshape() for my example in less steps/code than what I did below?
Any help is appreciated, thanks.
CODE TO PRODUCE INITIAL DATA SET
period <- 201801:201810
df <- data.frame(period)
df$store_id <- c(1,1,1,1,1,2,2,2,2,2)
df$product <- c(1,1,1,1,1,2,2,2,2,2)
df$price1 <- 11:20
df$price2 <- 5:14
df$price3 <- df_test$price1-df_test$price2
df$quantity1 <- 100
df$quantity2 <- 200
df$quantity3 <- 300
df
MY INEFFICIENT CODE / SOLUTION
# prices
df_prices <- df[,!names(df)%in%c("quantity1","quantity2","quantity3")]
df_prices <- reshape(df_prices,
direction = "long",
idvar = c("period", "product", "store_id"),
timevar = "type",
varying = c("price1","price2","price3"),
v.names = "price",
times = c("price1","price2","price3"))
# quantities
df_quantities <- df[,!names(df)%in%c("price1","price2","price3")]
df_quantities <- reshape(df_quantities,
direction = "long",
idvar = c("period", "product", "store_id"),
timevar = "type",
varying = c("quantity1","quantity2","quantity3"),
v.names = "quantity",
times = c("quantity1","quantity2","quantity3"))
# make variables consistent to merge
df_quantities$type[df_quantities$type=="quantity1"] <- "price1"
df_quantities$type[df_quantities$type=="quantity2"] <- "price2"
df_quantities$type[df_quantities$type=="quantity3"] <- "price3"
# merge both
df <- merge(df_prices, df_quantities, by=c("period","product","store_id","type"))