discretization in R with arules package

2020-07-13 08:29发布

问题:

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

回答1:

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



回答2:

This worked for me to discretize all columns:

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


回答3:

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

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