Product M
--------------
Brand1 2016m1
Brand1 2016m10
Brand1 2016m11
Brand1 2016m12
Brand1 2016m2
Brand1 2016m3
Brand1 2016m4
Brand1 2016m5
Brand1 2016m6
Brand1 2016m7
Brand1 2016m8
Brand1 2016m9
Brand2 2016m1
Brand2 2016m10
Brand2 2016m11
Brand2 2016m12
Brand2 2016m2
Brand2 2016m3
Brand2 2016m4
Brand2 2016m5
Brand2 2016m6
Brand2 2016m7
Brand2 2016m8
Brand2 2016m9
我有两个品牌Brand1和Brand2的时间序列。我的意图是像下面的表格一样转换此表。实际上,“产品”列应保持不变,而第二列M则应按升序进行转换,如下表所示。
Product M
------------
Brand1 2016m1
Brand1 2016m2
Brand1 2016m3
Brand1 2016m4
Brand1 2016m5
Brand1 2016m6
Brand1 2016m7
Brand1 2016m8
Brand1 2016m9
Brand1 2016m10
Brand1 2016m11
Brand1 2016m12
Brand2 2016m1
Brand2 2016m2
Brand2 2016m3
Brand2 2016m4
Brand2 2016m5
Brand2 2016m6
Brand2 2016m7
Brand2 2016m8
Brand2 2016m9
Brand2 2016m10
Brand2 2016m11
Brand2 2016m12
那么有人可以帮我改造这张桌子吗?
答案 0 :(得分:1)
我们可以在此处使用mixedsort
中的gtools
。
DF$M <- ave(DF$M, DF$Product, FUN = gtools::mixedsort)
# Product M
#1 Brand1 2016m1
#2 Brand1 2016m2
#3 Brand1 2016m3
#4 Brand1 2016m4
#5 Brand1 2016m5
#6 Brand1 2016m6
#7 Brand1 2016m7
#8 Brand1 2016m8
#9 Brand1 2016m9
#10 Brand1 2016m10
#11 Brand1 2016m11
#12 Brand1 2016m12
#13 Brand2 2016m1
#14 Brand2 2016m2
#15 Brand2 2016m3
#16 Brand2 2016m4
#17 Brand2 2016m5
#18 Brand2 2016m6
#19 Brand2 2016m7
#20 Brand2 2016m8
#21 Brand2 2016m9
#22 Brand2 2016m10
#23 Brand2 2016m11
#24 Brand2 2016m12