Add rows to grouped data with dplyr?

后端 未结 4 987
失恋的感觉
失恋的感觉 2020-12-15 07:32

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         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-15 08:31

    For this situation you can also use dcast and melt.

       library(dplyr)
       library(reshape2)
       data %>%
          dcast(Article ~ Week, value.var = "Demand", fun.aggregate = sum) %>%
          melt(id = "Article") %>%
          arrange(Article, variable)
    

提交回复
热议问题