2

I want to know how to remove curly brackets and "=>" this sign from association rules generated by apriori in order to export the rules in csv file.

Anup Kolte
  • 23
  • 3
  • 3
    Welcome to StackOverflow. In order to ask a better question please read [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) and [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). And see `help("sub")`. – Rui Barradas Apr 20 '19 at 19:14

2 Answers2

2

Package arules provides a function called DATAFRAME that can be used to suppress the brackets and place LHS and RHS into different columns.

 DATAFRAME(rules, setStart='', setEnd='', separate = TRUE)
Michael Hahsler
  • 2,965
  • 1
  • 12
  • 16
0

The structure returned by apriori contains the components (lhs, rhs) of the rules as factors. You can convert them to strings and remove the curly braces using gsub. Since you do not provide any data, I will illustrate with the built-in Adult data.

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" 
G5W
  • 36,531
  • 10
  • 47
  • 80