arules隔离lhs和rhs

时间:2016-02-28 16:17:37

标签: r arules

所以这是一个例子:

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,另一个名为ls,其中我存储了我的规则的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上无法正常工作......你有什么建议?

2 个答案:

答案 0 :(得分:1)

鉴于

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&amp; 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

答案 1 :(得分:1)

这是我打算这样做的方式......

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 = ""))