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

问题描述 投票:1回答:2

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

r csv associations rules apriori
2个回答
0
投票

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" 

0
投票

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

 DATAFRAME(rules, setStart='', setEnd='', separate = TRUE)
© www.soinside.com 2019 - 2024. All rights reserved.