Learn R Programming

arules (version 1.0-12)

categorize: Convert a Continuous Variable into a Categorical Variable

Description

This function converts a continuous variable into a categorical variable (factor) suitable for association rule mining.

Usage

categorize(x, breaks = 4, quantile = TRUE, labels = NULL, ...)

Arguments

x
a numeric vector (continuous variable).
breaks
number of categories or breaks for categories (quantiles or absolute values).
quantile
logical; use quantile (or absolute values) to determine the category boundaries.
labels
character vector; names for categories.
...
further arguments passed on to cut().

Value

  • A factor representing the categorized continuous variable.

See Also

cut in base and quantile in stats.

Examples

Run this code
data(iris)
head(iris)

### convert continuous variables into categories
### default (4 categories - 0-25%, 25-50%, 50-75% and 75-100% quantiles)
iris[,1] <- categorize(iris[,1])
### specify quantiles
iris[,2] <- categorize(iris[,2], breaks=c(0,.25,.75,1))
### specify absolute boundaries
iris[,3] <- categorize(iris[,3], breaks=c(0,1,2,3,4,5,6,7), quantile=FALSE)
### name categories
iris[,4] <- categorize(iris[,4], breaks=3, quantile=FALSE, 
	labels=c("short", "medium", "long"))

head(iris)

### convert dataset into transactions
tr <- as(iris, "transactions")
tr

### mine and inspect rules
rules<-apriori(tr)
inspect(head(sort(rules, by="lift")))

Run the code above in your browser using DataLab