ntiles: Split distribution into quantiles

View source: R/ntiles.R

ntilesR Documentation

Split distribution into quantiles

Description

The data of a variable are rank-ordered and split to bins of (approximately) equal size. When tied ranks span across category borders, the function assigns all values to the lowest possible bin. This procedure can result in slightly different results as the corresponding function Rank Cases of SPSS with option Ntiles.

Usage

ntiles(data, dv, 
       factors = NaN,
       bins = 5,
       res.labels = FALSE)

Arguments

data

A data frame containing the data relevant variable and possible factors that can be used to split the data frame into separate compartments.

dv

Character string specifying the name of the variable within data that is to be cut in bins. Alternatively, dv can be the appropriate column index.

factors

A string or vector of strings (e.g., c("subject","condition")) stating the conditions that should be used for splitting the data.

bins

The number of bins to be generated. Alternatively, a vector of cut-points can be specified according to the break argument of the function cut.

res.labels

The default value FALSE returns the bin number for each observation whereas TRUE returns the corresponding interval borders (in ranks).

Value

ntiles(data, dv, ...) returns a vector of bins.

Author(s)

Roland Pfister; Markus Janczyk

See Also

cut; rank; split; lapply;

Examples

## Build data frame
var1 <- c(1:9)
var2 <- c(1,1,1,2,2,2,3,3,3)
tmpdata <- data.frame(cbind(var1,var2))
tmpdata$var2 <- as.factor(tmpdata$var2)

## Get overall bins and display result
tmpdata$bins <- ntiles(tmpdata, dv = "var1", bins=3)
tmpdata

## Get bins separately for each factor level
## and display result
tmpdata$bins2 <- ntiles(tmpdata, dv = "var1", bins=3, factors = "var2")
tmpdata

schoRsch documentation built on Nov. 2, 2022, 1:06 a.m.

Related to ntiles in schoRsch...