【问题标题】:R associations rules with arules package - how to split rule formula into vector of elements?R 关联规则与 arules 包 - 如何将规则公式拆分为元素向量?
【发布时间】:2015-07-20 22:19:14
【问题描述】:

我有一个data.frame 对象,我通过这种方式将rules 类的对象转换为data.frame

trx.cpf.rules.df <- as(trx.cpf.rules, "data.frame")

(您可以从here 的结构中构建trx.cpf.rules.df 对象)。

这个数据框的头部是这样的:

> head(trx.cpf.rules.df)
                                                      rules   support confidence     lift
66 {Product_Group_1,Product_Group_49} => {Product_Group_48} 0.1060016  0.7371274 6.683635
12                 {Product_Group_48} => {Product_Group_49} 0.1067810  0.9681979 6.386621
68 {Product_Group_1,Product_Group_23} => {Product_Group_49} 0.1079501  0.9052288 5.971252
16                 {Product_Group_23} => {Product_Group_49} 0.1098987  0.8392857 5.536265
71 {Product_Group_1,Product_Group_23} => {Product_Group_34} 0.1024942  0.8594771 4.702384
19                 {Product_Group_34} => {Product_Group_23} 0.1079501  0.5906183 4.510496

是否有快速方法(专用函数或类似的东西)将每个trx.cpf.rules.df$rules 转换为两个包含relue;s 元素的向量?例如,对于第一行,它将是:

> (lhs.el <- c("Product_Group_1", "Product_Group_49"))
[1] "Product_Group_1"  "Product_Group_49"
> (rhs.el <- c("Product_Group_48"))
[1] "Product_Group_48"

【问题讨论】:

    标签: regex r dataframe associations arules


    【解决方案1】:

    这将为您提供带有 lhs/rhs 向量的 list 结构:

    l <- lapply( strsplit(as.character(trx.cpf.rules.df$rules), " => ", fixed = TRUE), function(x) {
      strsplit(  gsub("[{}]", "", x), ",", fixed = TRUE)
    })
    

    检查第一条规则:

    l[[1]]
    # [[1]]
    # [1] "Product_Group_1"  "Product_Group_49"
    # 
    # [[2]]
    # [1] "Product_Group_48"
    

    检查所有规则的左侧(头部):

    head(sapply(l, "[", 1))
    # [[1]]
    # [1] "Product_Group_1"  "Product_Group_49"
    # 
    # [[2]]
    # [1] "Product_Group_48"
    # 
    # [[3]]
    # [1] "Product_Group_1"  "Product_Group_23"
    # 
    # [[4]]
    # [1] "Product_Group_23"
    # 
    # [[5]]
    # [1] "Product_Group_1"  "Product_Group_23"
    # 
    # [[6]]
    # [1] "Product_Group_34"
    

    【讨论】:

    • 谢谢!我有一些关于包构建解决方案的想法,但找不到任何东西。我考虑了其他解决方案,但您的解决方案要方便得多。
    猜你喜欢
    • 1970-01-01
    • 2018-11-21
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2019-04-26
    相关资源
    最近更新 更多