chop: Convert a continuous variable to a discrete variable.

Description Usage Arguments Details Value See Also Examples

View source: R/chop.R

Description

Convert a continuous variable to a discrete variable.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
chop(x, ...)

## Default S3 method:
chop(x, ...)

## S3 method for class 'numeric'
chop(x, ...)

## S3 method for class 'data.frame'
chop(x, .factorVars, .skip, .m, .g, ...)

Arguments

x

A numeric or integer vector.

...

Additional arguments passed to hist() or Hmisc::cut2()

.m

(optional) Default value for the 'm' parameter to Hmisc::cut2()

.g

(optional) Default value for the 'g' parameter to Hmisc::cut2()

factorVars

(optional) A character vector of variable names in x to be directly converted to factors.

skip

(optional) A character vector of variable names in x to skip processing.

Details

When x is numeric, chop() assumes that any specified values for cuts, g, or m are exactly as they are required to be in cut2(). When x is a data frame, on the other hand, these parameters (and any other parameters passed to cut2() or hist()) are specified through parameters that have names which are common to the names of numeric varibles in x. (Any such specifications for non-numeric variables in x are ignored.) See examples for a demonstration of this usage. Note that .m and .g are overridden by any explicit values given for specific variables. See behavior for variable 'drat' in the final example.

Value

A factor the same length as x.

See Also

hist and Hmisc::cut2 for more details.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
table(chop(0:100))
table(chop(0:100000))
table(chop(0:100, breaks = c(-Inf,23,68,Inf))) #breaks passed to hist()
table(chop(0:100, cuts   = c(-Inf,23,68,Inf))) #same result, but cuts is passed to cut2()
table(chop(0:100, g=4))
table(chop(0:100, m=17))
summary(chop(mtcars, .m=10, .factorVars = c("cyl","vs","am","gear")))
z <-  chop(mtcars,
           .factorVars = c("cyl","vs","am","gear"),
           .skip = c("mpg"),
           .g = 4,
           drat = list(g=8),
           qsec = list(cuts=c(0,16,18,20,100)))
summary(z)
table(z$drat)

g_chop <- chop(glucose, glucose = list(cuts = c(0,109,125,300)))
table(g_chop$glucose, g_chop$cohort)

jarrod-dalton/jsd documentation built on Feb. 2, 2020, 12:23 p.m.