My data is in a data.frame format like this sample data:
data <- structure(list(Article = structure(c(1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
For this situation you can also use dcast and melt.
dcast
melt
library(dplyr) library(reshape2) data %>% dcast(Article ~ Week, value.var = "Demand", fun.aggregate = sum) %>% melt(id = "Article") %>% arrange(Article, variable)