arules 隔离 lhs 和 rhs

arules isolating lhs and rhs

下面是示例:

library(arules)
data(Adult)
rules <- apriori(Adult,parameter = list(maxlen = 2,minlen=2))
inspect(head(rules,3))


 lhs                         rhs                              support confidence     lift
1 {relationship=Unmarried} => {capital-loss=None}            0.1019819  0.9719024 1.019537
2 {occupation=Sales}       => {race=White}                   0.1005282  0.8920785 1.043314
3 {occupation=Sales}       => {native-country=United-States} 0.1039679  0.9226017 1.028055

我想创建一个包含 2 列的 data.table,一列称为 lhs,另一列称为 rhs,我在其中存储了我的规则的 lhs 和 rhs 值,如下所示:

lhs                        rhs
relationship=Unmarried    capital-loss=None
occupation=Sales          race=White
occupation=Sales          native-country=United-States

我发誓我曾在 windows 上以 a<-as.data.table(inspect(rules)) 开始,但在我的 mac 上它不起作用...你有什么建议?

给出

library(arules)
library(data.table)
data(Adult)
rules <- apriori(Adult,parameter = list(maxlen = 2,minlen=2))
r <- head(rules,3)

如果 as.data.table(inspect(r)) 不起作用(它在我的机器上起作用,Win7 x64,R 3.2.2 & packageVersion("arules") beeing 1.3.0),那么可以试试

f <- function(x, fun) unlist(as(fun(x), "list"))
( dt <- data.table(lhs=f(r, lhs), rhs=f(r, rhs)) )
#                       lhs                          rhs
# 1: relationship=Unmarried            capital-loss=None
# 2:       occupation=Sales                   race=White
# 3:       occupation=Sales native-country=United-States

这就是我想要完成的方式...

library(arules)
data(Adult)
rules <- apriori(Adult,parameter = list(maxlen = 2,minlen=2))

data.frame(lhs = labels(lhs(rules), setStart = "", setEnd = ""), 
           rhs = labels(rhs(rules), setStart = "", setEnd = ""))