重塑每周数据

我有一个数据集,其中包含不同饮料的每周销售额。该数据集包含从2019年1周到2020年18周的值。您可以在下面的代码中查看数据

        ARTEFICIAL_DATA<-data.frame(structure(list(year_week = c("2019 01", "2019 02", "2019 03", 
                                                                 "2019 04", "2019 05", "2019 06", "2019 07", "2019 08", "2019 09", 
                                                                 "2019 10", "2019 11", "2019 12", "2019 13", "2019 14", "2019 15", 
                                                                 "2019 16", "2019 17", "2019 18", "2019 19", "2019 20", "2019 21", 
                                                                 "2019 22", "2019 23", "2019 24", "2019 25", "2019 26", "2019 27", 
                                                                 "2019 28", "2019 29", "2019 30", "2019 31", "2019 32", "2019 33", 
                                                                 "2019 34", "2019 35", "2019 36", "2019 37", "2019 38", "2019 39", 
                                                                 "2019 40", "2019 41", "2019 42", "2019 43", "2019 44", "2019 45", 
                                                                 "2019 46", "2019 47", "2019 48", "2019 49", "2019 50", "2019 51", 
                                                                 "2019 52", "2020 01", "2020 02", "2020 03", "2020 04", "2020 05", 
                                                                 "2020 06", "2020 07", "2020 08", "2020 09", "2020 10", "2020 11", 
                                                                 "2020 12", "2020 13", "2020 14", "2020 15", "2020 16", "2020 17", 
                                                                 "2020 18"), Year = c(2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                                                                                      2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                                                                                      2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                                                                                      2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                                                                                      2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                                                                                      2019, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                                                                                      2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020), Pepsi = c(309, 
                                                                                                                                                 201, 369, 211, 262, 306, 477, 269, 175, 315, 373, 309, 280, 450, 
                                                                                                                                                 331, 394, 295, 266, 349, 434, 247, 302, 423, 347, 384, 327, 369, 
                                                                                                                                                 373, 380, 305, 432, 371, 437, 242, 270, 444, 432, 331, 309, 422, 
                                                                                                                                                 300, 495, 285, 408, 336, 484, 324, 327, 359, 441, 392, 373, 168, 
                                                                                                                                                 137, 404, 325, 279, 320, 461, 311, 336, 286, 333, 299, 209, 199, 
                                                                                                                                                 197, 344, 170, 176), Coke = c(139, 119, 189, 160, 184, 174, 246, 
                                                                                                                                                                               203, 117, 193, 233, 226, 219, 228, 219, 238, 220, 136, 198, 229, 
                                                                                                                                                                               172, 225, 191, 183, 224, 223, 205, 232, 228, 215, 193, 219, 205, 
                                                                                                                                                                               178, 172, 219, 206, 245, 234, 218, 180, 269, 220, 224, 211, 253, 
                                                                                                                                                                               231, 239, 225, 247, 270, 265, 68, 87, 211, 193, 200, 188, 247, 
                                                                                                                                                                               216, 226, 203, 231, 183, 143, 147, 143, 162, 125, 122), Fanta = c(24, 
                                                                                                                                                                                                                                                 18, 22, 20, 22, 20, 25, 23, 13, 22, 25, 24, 23, 31, 22, 23, 20, 
                                                                                                                                                                                                                                                 23, 19, 20, 19, 22, 22, 21, 26, 21, 27, 24, 22, 23, 21, 25, 19, 
                                                                                                                                                                                                                                                 21, 20, 22, 23, 23, 21, 32, 21, 28, 22, 23, 24, 24, 23, 26, 24, 
                                                                                                                                                                                                                                                 23, 28, 29, 7, 18, 23, 27, 25, 24, 27, 24, 29, 24, 24, 20, 17, 
                                                                                                                                                                                                                                                 21, 19, 13, 12, 16), Beer = c(145, 64, 158, 31, 56, 112, 206, 
                                                                                                                                                                                                                                                                               42, 45, 100, 114, 59, 38, 191, 89, 133, 56, 107, 131, 184, 56, 
                                                                                                                                                                                                                                                                               55, 211, 142, 134, 84, 137, 116, 130, 66, 218, 127, 213, 43, 
                                                                                                                                                                                                                                                                               78, 202, 203, 62, 54, 171, 99, 198, 44, 161, 100, 206, 70, 61, 
                                                                                                                                                                                                                                                                               110, 172, 94, 79, 93, 32, 171, 105, 54, 109, 187, 71, 81, 58, 
                                                                                                                                                                                                                                                                               78, 96, 49, 31, 34, 168, 33, 39), Juice = c(28, 40, 105, 26, 
                                                                                                                                                                                                                                                                                                                           41, 31, 96, 37, 20, 46, 78, 53, 29, 57, 46, 111, 49, 18, 58, 
                                                                                                                                                                                                                                                                                                                           112, 37, 43, 38, 93, 48, 69, 43, 57, 114, 43, 48, 47, 141, 35, 
                                                                                                                                                                                                                                                                                                                           47, 69, 158, 43, 49, 29, 34, 139, 40, 42, 38, 132, 38, 42, 41, 
                                                                                                                                                                                                                                                                                                                           103, 42, 48, 30, 22, 136, 39, 37, 39, 92, 40, 40, 37, 67, 73, 
                                                                                                                                                                                                                                                                                                                           31, 23, 27, 102, 31, 25), Lemonade = c(111, 22, 31, 0, 12, 77, 
                                                                                                                                                                                                                                                                                                                                                                  95, 0, 22, 50, 22, 2, 3, 129, 39, 0, 2, 84, 70, 42, 16, 6, 168, 
                                                                                                                                                                                                                                                                                                                                                                  31, 76, 10, 88, 37, 3, 17, 166, 73, 37, 5, 27, 128, 20, 5, 0, 
                                                                                                                                                                                                                                                                                                                                                                  136, 61, 32, 0, 113, 53, 52, 28, 14, 62, 54, 39, 22, 63, 8, 14, 
                                                                                                                                                                                                                                                                                                                                                                  63, 16, 69, 85, 26, 40, 19, 2, 17, 16, 8, 5, 50, 1, 13), Radler = c(2, 
                                                                                                                                                                                                                                                                                                                                                                                                                                      1, 21, 2, 1, 2, 11, 2, 1, 2, 12, 2, 3, 3, 2, 19, 2, 4, 2, 28, 
                                                                                                                                                                                                                                                                                                                                                                                                                                      2, 3, 3, 16, 8, 3, 3, 19, 11, 3, 2, 4, 34, 1, 3, 3, 22, 11, 3, 
                                                                                                                                                                                                                                                                                                                                                                                                                                      4, 1, 24, 1, 3, 7, 20, 1, 3, 3, 12, 8, 2, 1, 2, 21, 3, 2, 1, 
                                                                                                                                                                                                                                                                                                                                                                                                                                      10, 5, 1, 2, 10, 5, 2, 1, 2, 15, 1, 0)), row.names = c(NA, -70L
                                                                                                                                                                                                                                                                                                                                                                                                                                      ), class = c("tbl_df", "tbl", "data.frame")))

所以接下来应该如何转换此数据,如下面的图片

enter image description here

我尝试从reshape2包中使用melt函数,但是我得到的表不好。

ARTEFICIAL_DATA1<-melt(ARTEFICIAL_DATA,id.variables = 'year_week')

那么有人可以帮助重塑图片表格的数据(融化,旋转等)吗?

评论
贪婪的想念
贪婪的想念

We could use pivot_wider

library(dplyr)
library(tidyr)
ARTEFICIAL_DATA %>%  
   pivot_wider(names_from = Year, values_from = Pepsi:Radler)
点赞
评论