5

I am using arules package to discretize my continuous variables in data frame. I am using this particular line

discretize(data1,categories = 3)

but its giving me an error

Error in cut.default(x,k2) : k2 must be numeric

I am just trying to convert my continuous variables from "data1" data frame to 3 bins discrete variables. Any help would be appreciated...thanks in advance

Ankit Bhatia
  • 99
  • 1
  • 1
  • 8

3 Answers3

2

Check this code:

library(arules)
data1 <- sample(1:30,100,replace = T)
res <- discretize(data1,categories = 3)

It works correctly. Check

class(data1)

It should be integer or numeric

milos.ai
  • 3,882
  • 7
  • 31
  • 33
  • if data1 is data.frame then probably some column from data frame you want to discretize. e.g. res <- discretize(data1[[columnName]],categories = 3) – milos.ai Aug 19 '15 at 16:14
1

This worked for me to discretize all columns:

data1.Disc <- as.data.frame(lapply(data1, 
                                   function(x) discretize(x, categories=5)
                                   )
                           )
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
0

You can also use dplyr mutate_if function. This worked for me:

data1 <- data1 %>% mutate_if(is.numeric, funs(discretize(., method="frequency", categories=3)))
  • This is a fine approach, but OP's problem seems to be that his target variables are not numeric (and so `is_numeric`, in this case, will not be true). – cmaher Jan 18 '18 at 16:58
  • @cmaher disagree - OP's problem is that they called it *on the data frame* rather than on individual columns. – Gregor Thomas Nov 19 '19 at 03:16