简体   繁体   English

使用 arules 包在 R 中进行离散化

[英]discretization in R with arules package

I am using arules package to discretize my continuous variables in data frame.我正在使用 arules 包来离散数据框中的连续变量。 I am using this particular line我正在使用这条特殊的线路

discretize(data1,categories = 3)离散化(数据 1,类别 = 3)

but its giving me an error但它给了我一个错误

Error in cut.default(x,k2) : k2 must be numeric cut.default(x,k2) 中的错误:k2 必须是数字

I am just trying to convert my continuous variables from "data1" data frame to 3 bins discrete variables.我只是想将我的连续变量从“data1”数据帧转换为 3 个 bin 离散变量。 Any help would be appreciated...thanks in advance任何帮助将不胜感激...提前致谢

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它应该是整数数字

This worked for me to discretize all columns:这对我有用以离散化所有列:

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

You can also use dplyr mutate_if function.您还可以使用 dplyr mutate_if 函数。 This worked for me:这对我有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM