如何从 R 中的关联规则中删除大括号和 ''=>" 符号

How to remove curly brackets and ''=>" sign from association rules in R

我想知道如何从 apriori 生成的关联规则中删除大括号和“=>”这个符号,以便将规则导出到 csv 文件中。

apriori 返回的结构包含作为因子的规则组件(lhs、rhs)。您可以将它们转换为字符串并使用 gsub 删除大括号。由于您不提供任何数据,我将使用内置 Adult 数据进行说明。

library(arules)

data("Adult")
rules <- apriori(Adult, 
    parameter = list(supp = 0.7, conf = 0.9, target = "rules"))

LHS = gsub("[{}]", "", as.character(inspect(rules@lhs)$items))
RHS = gsub("[{}]", "", as.character(inspect(rules@rhs)$items))

LHS
 [1] ""                                    ""  
[3] "race=White"                           "race=White"            
 [5] "race=White"                          "native-country=UnitedStates"                  
 [7] "native-country=United-States"        "capital-gain=None"                             
 [9] "capital-loss=None"                   "race=White,native-country=United-States"       
[11] "race=White,capital-gain=None"        "race=White,native-country=United-States"       
[13] "race=White,capital-loss=None"        "race=White,capital-gain=None"                  
[15] "race=White,capital-loss=None"        "capital-gain=None,native-country=United-States"

RHS
 [1] "capital-gain=None"        "capital-loss=None"            "native-country=United-States"
 [4] "capital-gain=None"        "capital-loss=None"            "capital-gain=None"           
 [7] "capital-loss=None"        "capital-loss=None"            "capital-gain=None"           
[10] "capital-gain=None"        "native-country=United-States" "capital-loss=None"           
[13] "native-country=United-States" "capital-loss=None"        "capital-gain=None"           
[16] "capital-loss=None"        "capital-gain=None" 

软件包 arules 提供了一个名为 DATAFRAME 的函数,可用于取消括号并将 LHS 和 RHS 放入不同的列中。

 DATAFRAME(rules, setStart='', setEnd='', separate = TRUE)