Discretization in R with arules package

I use the arules package to discrete my continuous variables in a data frame. I am using this particular line

discretize (data1, categories = 3)

but it gives me an error

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

I'm just trying to convert my continuous variables from a data1 data frame to 3 bin of discrete variables. Any help would be appreciated ... thanks in advance

+4
source share
3 answers

This worked for me:

data1.Disc <- as.data.frame(lapply(data1, 
                                   function(x) discretize(x, categories=5)
                                   )
                           )
+1
source

Check this code:

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

It is working correctly. Check out

class(data1)

It must be an integer or numeric

0

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

data1 <- data1 %>% mutate_if(is.numeric, funs(discretize(., method="frequency", categories=3)))
0
source

All Articles