Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

ncut

Function to group a variable in intervals.


Description

Cuts a continuous variable in intervals. As opposed to cut which returns a factor, ncut returns a numeric variable.

Usage

ncut(x, breaks, type="left" )

Arguments

x

A numerical vector.

breaks

Vector of breakpoints. NA will results for values below min(breaks) if type="left", for values above max(breaks) if type="right" and for values outside range(breaks) if type="mid"

type

Character: one of c("left","right","mid"), indicating whether the left, right or midpoint of the intervals defined in breaks is returned.

Details

The function uses the base function findInterval.

Value

A numerical vector of the same length as x.

Author(s)

Bendix Carstensen, Steno Diabetes Center, b@bxc.dk, http://bendixcarstensen.com, with essential input from Martyn Plummer, martyn.plummer@r-project.org

See Also

Examples

br <- c(-2,0,1,2.5)
x <- c( rnorm( 10 ), br, -3, 3 )
cbind( x, l=ncut( x, breaks=br, type="l" ),
          m=ncut( x, breaks=br, type="m" ),
          r=ncut( x, breaks=br, type="r" ) )[order(x),]
x <- rnorm( 200 )
plot( x, ncut( x, breaks=br, type="l" ), pch=16, col="blue", ylim=range(x) )
abline( 0, 1 )
abline( v=br )
points( x, ncut( x, breaks=br, type="r" ), pch=16, col="red" )
points( x, ncut( x, breaks=br, type="m" ), pch=16, col="green" )

Epi

Statistical Analysis in Epidemiology

v2.44
GPL-2
Authors
Bendix Carstensen [aut, cre], Martyn Plummer [aut], Esa Laara [ctb], Michael Hills [ctb]
Initial release
2021-02-28

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.