Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
894 views
in Technique[技术] by (71.8m points)

r - how to convert data.frame to transactions for 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")

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...