0

所以这里是一个例子:

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

我发誓我曾经a<-as.data.table(inspect(rules))在 Windows 上做过一次,但在我的 Mac 上却不起作用......你有什么建议?

4

2 回答 2

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 & 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
于 2016-02-28T17:01:19.243 回答
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 = ""))
于 2016-02-29T02:38:57.307 回答