Learn R Programming

pedometrics (version 0.7.0)

cont2cat: Categorize/stratify continuous variable(s)

Description

Create break points, compute strata proportions, and stratify continuous variable(s) to create categorical variable(s).

Usage

cont2cat(x, breaks, integer = FALSE)

breakPoints(x, n, type = "area", prop = FALSE)

stratify(x, n, type = "area", integer = FALSE)

Arguments

x

Vector, data frame or matrix with data on the continuous variable(s) to be categorized/stratified.

breaks

Vector or list containing the lower and upper limits that should be used to break the continuous variable(s) into categories. See ‘Details’ for more information.

integer

Logical value indicating if the categorical variable(s) be returned as integers. Defaults to integer = FALSE, i.e. the variable(s) will be returned as factors.

n

Integer value indicating the number of strata that should be created.

type

Character value indicating the type of strata that should be used, with options "area", for equal-area, and "range", for equal-range strata. Defaults to type = "area".

prop

Logical value indicating if the strata proportions should be returned? Defaults to prop = FALSE.

Value

A vector, data frame, or matrix, depending on the class of x.

Details

Argument breaks must be a vector if x is a vector, but a list if x is a data frame or matrix. Using a list allows breaking each column of x into different number of categories.

See Also

cut2

Examples

Run this code
# NOT RUN {
## Compute the break points of marginal strata
x <- data.frame(x = round(rnorm(10), 1), y = round(rlnorm(10), 1))
x <- breakPoints(x = x, n = 4, type = "area", prop = TRUE)
x

## Convert continuous data into categorical data
# Matrix
x <- y <- c(1:10)
x <- cbind(x, y)
breaks <- list(c(1, 2, 4, 8, 10), c(1, 5, 10))
y <- cont2cat(x, breaks)
y
# Data frame
x <- y <- c(1:10)
x <- data.frame(x, y)
breaks <- list(c(1, 2, 4, 8, 10), c(1, 5, 10))
y <- cont2cat(x, breaks, integer = TRUE)
y
# Vector
x <- c(1:10)
breaks <- c(1, 2, 4, 8, 10)
y <- cont2cat(x, breaks, integer = TRUE)
y

## Stratification
x <- data.frame(x = round(rlnorm(10), 1), y = round(rnorm(10), 1))
x <- stratify(x = x, n = 4, type = "area", integer = TRUE)
x
# }

Run the code above in your browser using DataLab