discretization in R with arules package
Asked Answered
S

3

6

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

Sheilasheilah answered 19/8, 2015 at 16:0 Comment(1)
can you show us your data1?Rountree
R
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

Rountree answered 19/8, 2015 at 16:9 Comment(1)
if data1 is data.frame then probably some column from data frame you want to discretize. e.g. res <- discretize(data1[[columnName]],categories = 3)Rountree
M
1

This worked for me to discretize all columns:

data1.Disc <- as.data.frame(lapply(data1, 
                                   function(x) discretize(x, categories=5)
                                   )
                           )
Metapsychology answered 31/3, 2017 at 7:25 Comment(0)
P
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)))
Pegu answered 18/1, 2018 at 16:51 Comment(2)
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).Moskowitz
@Moskowitz disagree - OP's problem is that they called it on the data frame rather than on individual columns.Bekha

© 2022 - 2024 — McMap. All rights reserved.