[R] Need help to convert data frame to transaction set.
arun
smartpink111 at yahoo.com
Sun Apr 20 07:36:12 CEST 2014
Hi Satish,
Using your code:
part<-read.csv("spares.csv")
str(part)
#'data.frame': 838 obs. of 1 variable:
# $ Cust_ID.Parts: Factor w/ 838 levels "100\tAIR\tFILTER\tHOUSING\t",..: 161 333 495 727 728 768 769 770 784 785 ...
#I guess you have only two columns in the dataset
lines1 <- readLines("spares.csv")
head(lines1)
#[1] "Cust_ID\tParts\t\t\t" "1\tFENDERS\t\t\t"
#[3] "2\tFENDERS\t\t\t" "3\tOIL\tFILTERS\t\t"
#[5] "4\tBMW\tSERVICE\tFLUIDS\t" "4\tOIL\tFILTER\t\t"
part<-read.csv("spares.csv",header=TRUE,sep="\t")
str(part)
#'data.frame': 838 obs. of 5 variables:
# $ Cust_ID: int 1 2 3 4 4 5 5 5 6 6 ...
# $ Parts : Factor w/ 26 levels "AIR","ALTERNATOR",..: 9 9 16 4 16 6 9 12 6 9 ...
# $ X : Factor w/ 14 levels "","BLADES","CAR",..: 1 1 6 14 5 1 1 1 1 1 ...
# $ X.1 : Factor w/ 6 levels "","(AXLE","CARE",..: 1 1 1 4 1 1 1 1 1 1 ...
# $ X.2 : Factor w/ 2 levels "","BOOT)": 1 1 1 1 1 1 1 1 1 1 ...
part$Parts <- interaction(part[,2:5],sep=" ",drop=TRUE)
part <- part[,1:2]
str(part)
#'data.frame': 838 obs. of 2 variables:
# $ Cust_ID: int 1 2 3 4 4 5 5 5 6 6 ...
# $ Parts : Factor w/ 31 levels "ALTERNATOR ",..: 6 6 21 28 20 3 6 8 3 6 ...
as(split(part[,"Cust_ID"],part[,"Parts"]),"transactions")
#transactions in sparse format with
# 31 transactions (rows) and
# 502 items (columns)
A.K.
On Saturday, April 19, 2014 11:08 PM, Sathish Kumar <tellsathish at gmail.com> wrote:
Hi AK,
I have attached the data set.
And as you suggested, I coded as follows but still I am getting an error
> part<-read.csv("spares.csv")
> part
>lst1<-split(part[,"Cust_ID"],part[,"Parts"])
> as(lst1, "transactions")
Error in as(lst1, "transactions") :
no method or default for coercing “list” to “transactions”
On Sat, Apr 19, 2014 at 6:43 PM, arun <smartpink111 at yahoo.com> wrote:
>
>Hi,
>
>Without a reproducible example using ?dput() or the package name, it is a bit difficult to comment.
>
>Assuming that you used:
>library(arules)
> data("AdultUCI")
>AdultUCI$ID <- 1:nrow(AdultUCI)
>
>lst1 <- split(AdultUCI[,"ID"], AdultUCI[,"marital-status"])
> as(lst1, "transactions")
>#transactions in sparse format with
># 7 transactions (rows) and
># 48842 items (columns)
>
>
>
>A.K.
>
>
>
>On Saturday, April 19, 2014 5:01 PM, Sathish Kumar <tellsathish at gmail.com> wrote:
>Hi,
>
>To convert coerce the data set to transaction data set I used the code
>
>trans4 <- as(split(a[,"Cust_ID"], a[,"Parts"]), "transactions")
>
>but I am getting the following error-
>
>Error in as(split(a[, "Cust_ID"], a[, "Parts"]), "transactions") : nomethod
>or default for coercing “list” to “transactions”
>
>
>
>Then I tried first converting the data set to matrix structure using the
>code
>
>c_m<-as.matrix(c_df)
>c_m
>
>
>
>then entered the following code
>
>
>trans2 <- as(c_m, "transactions")
>
>but got the following error
>
>Error in as(c_m, "transactions") : no method or default for coercing
>“matrix” to “transactions”
>Please let me know how to correct the problem.
>
>Thanks
>Sathish
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list