R: Convert Data Frame to Transactions to Use ARules Package

2.7k Views Asked by At

I have a csv file that looks like the below data frame once it is imported:

df <- read("data.csv")

ID  Name  Flag1 Flag2 Flag3 Flag4
1    a      0    F2     F3   F4
2    b     F1     0     F3   F4
3    c      0    F2     F3    0
4    d      0    F2     0    F4
5    e     F1    F2     F3    0

ID and Name are factors. Flag1 through Flag4 are chr.

I need to use the rules package, so I need to convert this data frame into a transaction file. I know that to do so, I need to convert all values to factors, so I did this:

as(as.factor(Flags), "transactions")

which gave me this:

Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

Then I tried reading the csv file directly:

read.transactions("data.csv", format = "basket", sep = ",")

which gave me this:

Error in asMethod(object) : 
  can not coerce list with transactions with duplicated items

I'm thinking this is because of the 0's in the data, but I can't figure out a way to remove the 0's. To use functions in the rules packages, I need the dataset to look like this, correct?

1,a,F2,F3,F4
2,b,F1,F3,F4
3,c,F2,F3
4,d,F2,F4
5,e,F1,F2,F3

Thanks ahead of time.

1

There are 1 best solutions below

0
On

Try reading the data in the following manner:

required_Data<-read.transactions("data.csv", rm.duplicates=TRUE, format = "basket", sep = ",")

Hope that helps!