Kaja
Kaja

Reputation: 3057

Extracting special rules in association analysis

How can I extarct rules which in lhsonly 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

Upvotes: 1

Views: 326

Answers (2)

lukeA
lukeA

Reputation: 54237

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% item & size(lhs)==1)
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

Upvotes: 3

Sandipan Dey
Sandipan Dey

Reputation: 23101

Try this (assuming the rules are generated using apriori):

df <- as(rules, 'data.frame')
df$rules <- as.character(df$rules)
lhs <- do.call(rbind, strsplit(df$rules, split='=>'))[,1]
lhs.items <- strsplit(lhs, split=',')
indices <- which(lapply(lhs.items, length) == 1)
special.item <- '231050'
special.indices <- which(grepl(special.item, lhs.items[[indices]]))
selected.rules <- df[special.indices,]
selected.rules

  rules     support  confidence          lift
1 {231050}=>{231051} 0.06063479          1 16.49218

Upvotes: 0

Related Questions