Kaja Kaja - 2 months ago 20
R Question

Extracting special rules in association analysis

How can I extarct rules which in

lhs
only one special Item appear

1 {231050} => {231051} 0.06063479 1.0000000 16.492183
2 {231050,231051} => {275001} 0.05490568 0.9055145 6.576661
3 {231050,275001} => {231051} 0.05490568 1.0000000 16.492183


I would like to extract only first row in which I have only one
231050

Answer

arules has a subset function (see ?arules::subset), which you can use to draw a subset of rules meeting your criteria - like specific items on lhs, minimum support etc:

library(arules)
data("Adult")
rules <- apriori(Adult, parameter = list(supp = 0.5, conf = 0.9, minlen = 2))
item <- "race=White"
rules.sub <- subset(rules, !lhs %in% setdiff(itemLabels(rules), item))
inspect(rules.sub)
#   lhs             rhs                            support   confidence lift     
# 7 {race=White} => {native-country=United-States} 0.7881127 0.9217231  1.0270761
# 8 {race=White} => {capital-gain=None}            0.7817862 0.9143240  0.9966616
# 9 {race=White} => {capital-loss=None}            0.8136849 0.9516307  0.9982720