如何data.frame转换为arules交易 [英] how to convert data.frame to transactions for arules

查看:1592
本文介绍了如何data.frame转换为arules交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个CSV文件中读取数据,该数据有3列,一类是事务ID,另外两个是产品和产品的产品类别。我需要将其转换成交易,以便使用先验函数的 arules 。它显示了一个错误,当我转换为交易:

I read data from a csv file, the data has 3 columns, one is transaction id, the other two are product and product catagory. I need to convert this into transactions in order to use the apriori function in arules. It shows an error when I convert to transactions:

dat <- read.csv("spss.csv",head=TRUE,sep="," , as.is = T)
dat[,2] <- factor(dat[,2])
dat[,3] <- factor(dat[,3])
spssdat <- dat[,c(1,2,3)]
str(spssdat)

'data.frame':   108919 obs. of  3 variables:
 $ Transaction_id: int  3000312 3000312 3001972 3003361 3003361 3003361 3003361 3003361 3003361 3004637 ...
 $ product_catalog : Factor w/ 9 levels "AIM","BA","IM",..: 1 1 5 7 7 7 7 7 7 1 ...
 $ product      : Factor w/ 332 levels "ACM","ACTG/AIM",..: 7 7 159 61 61 61 61 61 61 7 ...

trans4 <- as(spssdat, "transactions")

Error in as(spssdat, "transactions") : 
  no method or default for coercing "data.frame" to "transactions"

如果数据只有两列,它可以工作方式:

If the data only have two columns, it can work by:

trans4 <- as(split(spssdat[,2], spssdat[,1]), "transactions")

但我不知道怎么当我有3列转换。通常有更多列喜欢的类别属性,客户属性。所以列通常比2列大。需要找到多个列之间的规则。

But I don't know how to convert when I have 3 columns. Usually there are the additional columns likes category attributes, customer attributes. so the column usually large than 2 columns. need to find rules between multiple columns.

推荐答案

我发现,我对工作的本网站。让我复制相关段落:

I have found some information that worked for me on this website. Let me copy relevant paragraph:

数据框可以在任何一个标准化的()形式或平面文件(篮下)的形式。结果
  当文件中的篮下形式,它意味着每个记录重新presents交易,其中在篮下的项目将重新被列psented $ P $

  当数据集时,形式,它意味着每个记录重新presents一个单一的项目,每个项目包含一个事务ID 即可。

The dataframe can be in either a normalized (single) form or a flat file (basket) form.
When the file is in basket form it means that each record represents a transaction where the items in the basket are represented by columns.
When the dataset is in single form it means that each record represents one single item and each item contains a transaction id.

要从文件加载交易,使用 read.transactions 。在这两个你和我的情况的文件是在的形式。结果
我用下面的code加载.csv文件为交易

To load transactions from file, use read.transactions. In both your and my case file is in the single form.
I've used following code to load .csv file as transactions:

trans = read.transactions("some_data.csv", format = "single", sep = ",", cols = c("transactionID", "productID"))

要充分理解上面的命令,看看 read.transactions 手册,可键入后?read.transactions R中的控制台。

To fully understand above command, take a look at read.transactions manual, available after typing ?read.transactions in R console.

这篇关于如何data.frame转换为arules交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆