0

I have .csv data with column and 5000 rows like as follows:

age sex children distance  
  1   0        1        1    
  1   1        1        2   
  2   1        0        1    
  1   0        1        3    
  3   1        0        2   
  1   1        1        1 

I want to apply association rule mining using apriori(). So tried to read the dataset using read.transactions as follows:

mar = read.transactions("Marketing campaign.csv", format = "basket", 
                        sep = ",", cols = c("age", "sex", "children", "distance"))  

But I get the following error:

Error in read.transactions("Marketing campaign.csv", format = "basket", : 'cols' must be a numeric scalar for 'basket'.

Please help. Thanks in Advance

micstr
  • 5,080
  • 8
  • 48
  • 76
Abhi
  • 1
  • 1
  • 2

1 Answers1

1

In R, the data type that holds categrorial variables is called factor. Factor vectors can easily be created with the methods as.factor and factor

myDf <- data.frame(
  age = c(1,1,2,1,3,1),
  sex = c(1,1,1,0,1,1),
  children = c(1,1,0,1,0,1),
  distance = c(0,2,1,3,2,1)
)

myDf
#   age sex children distance
# 1   1   1        1        0
# 2   1   1        1        2
# 3   2   1        0        1
# 4   1   0        1        3
# 5   3   1        0        2
# 6   1   1        1        1

myDf$sex <- factor(myDf$sex, labels = c("F", "M"))

myDf
#   age sex children distance
# 1   1   M        1        0
# 2   1   M        1        2
# 3   2   M        0        1
# 4   1   F        1        3
# 5   3   M        0        2
# 6   1   M        1        1
Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43
  • But I have 5000 rows of this combination. And after that, I need to apply apriori() on that. Even after converting in factors, it showing that no method or default for coercing “tbl_df” to “transactions” – Abhi Jul 03 '17 at 07:38
  • I don't understand how the number of rows makes a difference here. About `transactions`: This is a completely different question than the one you asked previously. In the `arules` pasckage, there is a specific class called `transactions`. If you want help with that, mention this in the question – Gregor de Cillia Jul 03 '17 at 08:06
  • Is it possible that `as.transactions` works on data frames, but not `tbl_df`s? – sebastianmm Jul 25 '17 at 20:22