按组总和排序
Order by group sum
以 ggplot 中的钻石数据集为例,我想按每种颜色的总价对数据框进行排序,例如,如果这是每种颜色的总价
H 5000
I 4000
E 1000
J 3000
我想对原始数据框进行排序以按照上面相同的顺序显示颜色
所以如果我们有这样的原始数据集的样本
carat cut color clarity depth table price
0.23 Ideal E SI2 61.5 55 326
0.21 Premium E SI1 59.8 61 326
0.23 Good E VS1 56.9 65 327
0.29 Premium I VS2 62.4 58 334
0.31 Good J SI2 63.3 58 335
0.24 Very Good J VVS2 62.8 57 336
0.24 Very Good I VVS1 62.3 57 336
0.26 Very Good H SI1 61.9 55 337
它应该被排序成这样
carat cut color clarity depth table price
0.26 Very Good H SI1 61.9 55 337
0.24 Very Good I VVS1 62.3 57 336
0.29 Premium I VS2 62.4 58 334
0.23 Ideal E SI2 61.5 55 326
0.21 Premium E SI1 59.8 61 326
0.23 Good E VS1 56.9 65 327
0.31 Good J SI2 63.3 58 335
0.24 Very Good J VVS2 62.8 57 336
因为H色总价最高然后I色以此类推
我可以按每种颜色的总价订购颜色,但我想订购
数据本身按颜色设置
如上所述。
如果我们可以为每条记录再按价格排序会更好,这样会像这样
carat cut color clarity depth table price
0.26 Very Good H SI1 61.9 55 337
0.24 Very Good I VVS1 62.3 57 336
0.29 Premium I VS2 62.4 58 334
0.23 Good E VS1 56.9 65 327
0.23 Ideal E SI2 61.5 55 326
0.21 Premium E SI1 59.8 61 326
0.24 Very Good J VVS2 62.8 57 336
0.31 Good J SI2 63.3 58 335
我不确定我是否完全理解你想要的顺序,但如果是先按价格再按颜色,都按降序排列,那么你可以这样做:
library('ggplot2')
library('dplyr')
tst <- dplyr::arrange(diamonds, desc(color), desc(price))
其中给出,对于 head(tst)
:
carat cut color clarity depth table price x y z
1 3.01 Premium J SI2 60.7 59 18710 9.35 9.22 5.64
2 3.01 Premium J SI2 59.7 58 18710 9.41 9.32 5.59
3 2.22 Premium J VS1 60.0 60 18706 8.49 8.43 5.08
4 3.51 Premium J VS2 62.5 59 18701 9.66 9.63 6.03
5 2.43 Premium J VS2 62.2 57 18692 8.63 8.54 5.34
6 2.42 Premium J VS2 61.3 59 18615 8.61 8.58 5.27
有了data.table
我会做以下事情(使用你提供的数据)
library(data.table)
# convert to `data.table` and assign a TotSum column (per color) by reference
setDT(df)[, TotSum := sum(price), by = color]
# sort your data by total sum (decreasing), color (in case two colors will have the same total price) and by price (decreasing)
setorder(df, -TotSum, color, -price)
df
# carat cut color clarity depth table price TotSum
# 1: 0.23 Good E VS1 56.9 65 327 979
# 2: 0.23 Ideal E SI2 61.5 55 326 979
# 3: 0.21 Premium E SI1 59.8 61 326 979
# 4: 0.24 Very Good J VVS2 62.8 57 336 671
# 5: 0.31 Good J SI2 63.3 58 335 671
# 6: 0.24 Very Good I VVS1 62.3 57 336 670
# 7: 0.29 Premium I VS2 62.4 58 334 670
# 8: 0.26 Very Good H SI1 61.9 55 337 337
这里我们为每种颜色创建了一个新的列TotSum
,并按照df
参考按总金额、颜色和价格排序df
每种颜色。
以 ggplot 中的钻石数据集为例,我想按每种颜色的总价对数据框进行排序,例如,如果这是每种颜色的总价
H 5000
I 4000
E 1000
J 3000
我想对原始数据框进行排序以按照上面相同的顺序显示颜色
所以如果我们有这样的原始数据集的样本
carat cut color clarity depth table price
0.23 Ideal E SI2 61.5 55 326
0.21 Premium E SI1 59.8 61 326
0.23 Good E VS1 56.9 65 327
0.29 Premium I VS2 62.4 58 334
0.31 Good J SI2 63.3 58 335
0.24 Very Good J VVS2 62.8 57 336
0.24 Very Good I VVS1 62.3 57 336
0.26 Very Good H SI1 61.9 55 337
它应该被排序成这样
carat cut color clarity depth table price
0.26 Very Good H SI1 61.9 55 337
0.24 Very Good I VVS1 62.3 57 336
0.29 Premium I VS2 62.4 58 334
0.23 Ideal E SI2 61.5 55 326
0.21 Premium E SI1 59.8 61 326
0.23 Good E VS1 56.9 65 327
0.31 Good J SI2 63.3 58 335
0.24 Very Good J VVS2 62.8 57 336
因为H色总价最高然后I色以此类推
我可以按每种颜色的总价订购颜色,但我想订购 数据本身按颜色设置
如上所述。
如果我们可以为每条记录再按价格排序会更好,这样会像这样
carat cut color clarity depth table price
0.26 Very Good H SI1 61.9 55 337
0.24 Very Good I VVS1 62.3 57 336
0.29 Premium I VS2 62.4 58 334
0.23 Good E VS1 56.9 65 327
0.23 Ideal E SI2 61.5 55 326
0.21 Premium E SI1 59.8 61 326
0.24 Very Good J VVS2 62.8 57 336
0.31 Good J SI2 63.3 58 335
我不确定我是否完全理解你想要的顺序,但如果是先按价格再按颜色,都按降序排列,那么你可以这样做:
library('ggplot2')
library('dplyr')
tst <- dplyr::arrange(diamonds, desc(color), desc(price))
其中给出,对于 head(tst)
:
carat cut color clarity depth table price x y z
1 3.01 Premium J SI2 60.7 59 18710 9.35 9.22 5.64
2 3.01 Premium J SI2 59.7 58 18710 9.41 9.32 5.59
3 2.22 Premium J VS1 60.0 60 18706 8.49 8.43 5.08
4 3.51 Premium J VS2 62.5 59 18701 9.66 9.63 6.03
5 2.43 Premium J VS2 62.2 57 18692 8.63 8.54 5.34
6 2.42 Premium J VS2 61.3 59 18615 8.61 8.58 5.27
有了data.table
我会做以下事情(使用你提供的数据)
library(data.table)
# convert to `data.table` and assign a TotSum column (per color) by reference
setDT(df)[, TotSum := sum(price), by = color]
# sort your data by total sum (decreasing), color (in case two colors will have the same total price) and by price (decreasing)
setorder(df, -TotSum, color, -price)
df
# carat cut color clarity depth table price TotSum
# 1: 0.23 Good E VS1 56.9 65 327 979
# 2: 0.23 Ideal E SI2 61.5 55 326 979
# 3: 0.21 Premium E SI1 59.8 61 326 979
# 4: 0.24 Very Good J VVS2 62.8 57 336 671
# 5: 0.31 Good J SI2 63.3 58 335 671
# 6: 0.24 Very Good I VVS1 62.3 57 336 670
# 7: 0.29 Premium I VS2 62.4 58 334 670
# 8: 0.26 Very Good H SI1 61.9 55 337 337
这里我们为每种颜色创建了一个新的列TotSum
,并按照df
参考按总金额、颜色和价格排序df
每种颜色。