0

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

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

(您可以从此处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"
4

1 回答 1

2

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

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"
于 2015-05-10T10:35:27.877 回答