如何按R中的lhs或rhs对关联规则进行排序

时间:2016-11-17 14:15:24

标签: r sorting arules

我有apriori算法的一套规则。通过提升,信心或支持对它们进行排序很容易:

rules.sorted = sort(rules, by="lift")

但是,让我说我有一些规则与一些不同的rhs元素。 我想查看按这些rhs元素排序的规则(按字母顺序排列)。有没有办法做到这一点?

I.e from this:

    lhs       rhs support   confidence lift    
1   {A}    => {B} 0.3919252 0.9431280  1.930940
2   {B}    => {A} 0.3919252 0.8024194  1.930940
3   {E,C}  => {A} 0.3535204 0.7995546  1.924047
4   {F,I}  => {F} 0.3924175 0.9005650  1.868281
5   {H}    => {G} 0.4194978 0.9659864  1.864941
6   {C,D}  => {A} 0.3653373 0.7141482  1.718525

对此:

    lhs       rhs support   confidence lift    

2   {B}    => {A} 0.3919252 0.8024194  1.930940
3   {E,C}  => {A} 0.3535204 0.7995546  1.924047
6   {C,D}  => {A} 0.3653373 0.7141482  1.718525
1   {A}    => {B} 0.3919252 0.9431280  1.930940
4   {F,I}  => {F} 0.3924175 0.9005650  1.868281
5   {H}    => {G} 0.4194978 0.9659864  1.864941

以下是我使用的规则示例:

library(arules)
download.file("http://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data", "mush.data");
dataset = read.table("mush.data", header = F, sep=",", na.strings= "*")
tr <- as(dataset, "transactions")
param  = new("APparameter", "confidence" = 0.9, "support" = 0.7, "minlen"= 2L, "maxlen" = 2L, "target" = "rules") 
rules <-apriori(tr,param)
dput(rules)

2 个答案:

答案 0 :(得分:2)

rules对象的基本信息提取到数据框,如下所示:

rules_info <-
  data.frame(
    LHS = labels(lhs(rules)), 
    RHS = labels(rhs(rules)),          
    quality(rules)
  )

然后将其排序为普通数据框:

rules_info[ order(rules_info$RHS), ]

答案 1 :(得分:2)

bergant的解决方案正是您所需要的。我只是直接重新排序规则对象中的规则:

rules_sorted <- rules[order(labels(rhs(rules)))]

现在对新规则对象进行了排序。