ntile: Membership of ntile groups

Description Usage Arguments Value Note Author(s) See Also Examples

Description

Creates groups where the groups each have as close to the same number of members as possible.

Usage

1
2
ntile(x, ngroups, na.rm = FALSE, result = "list",
    reverse = FALSE, checkBleed = TRUE)

Arguments

x

a numeric vector.

ngroups

a positive integer giving the number of groups to create.

na.rm

logical value: if TRUE, then missing values are removed before forming the groups.

result

a character string specifying the form of the resulting value. This must be (an abbreviation of) one of: "list", "numeric", "factor"

reverse

logical value: if TRUE, then the biggest values are in group 1.

checkBleed

logical value: if TRUE, then a warning is issued if the same value appears in more than one group.

Value

the form of the value depends on the result argument.

The "list" result has the values of the input x grouped into ngroups components.

The "numeric" result is a vector of integers from 1 to ngroups indicating which group the corresponding element of x is in.

The "factor" result is an ordered factor version of the "numeric" result.

Note

A more primitive version of this function appeared in a blog post called "Miles of iles" on the Portfolio Probe website. A bit of comparison with the alternative functions in See Also may be found there.

Author(s)

Pat Burns

See Also

cut, cut2, quantcut.

Examples

1
2
3
4
5
ntile(setNames(state.area, state.name), 10)

ntile(Loblolly$height, 5, result="factor", reverse=TRUE)

ntile(c(-10:10, rep(0, 7)), 4)

Example output

$`1`
 Connecticut     Delaware       Hawaii   New Jersey Rhode Island 
        5009         2057         6450         7836         1214 

$`2`
     Maryland Massachusetts New Hampshire       Vermont West Virginia 
        10577          8257          9304          9609         24181 

$`3`
       Indiana       Kentucky          Maine South Carolina       Virginia 
         36291          40395          33215          31055          40815 

$`4`
   Louisiana  Mississippi         Ohio Pennsylvania    Tennessee 
       48523        47716        41222        45333        42244 

$`5`
       Alabama       Arkansas       New York North Carolina      Wisconsin 
         51609          53104          49576          52586          56154 

$`6`
 Florida  Georgia Illinois     Iowa Michigan 
   58560    58876    56400    56290    58216 

$`7`
    Missouri North Dakota     Oklahoma South Dakota   Washington 
       69686        70665        69919        77047        68192 

$`8`
    Idaho    Kansas Minnesota  Nebraska      Utah 
    83557     82264     84068     77227     84916 

$`9`
 Arizona Colorado   Nevada   Oregon  Wyoming 
  113909   104247   110540    96981    97914 

$`10`
    Alaska California    Montana New Mexico      Texas 
    589757     158693     147138     121666     267339 

 [1] 5 4 3 2 2 1 5 4 3 2 1 1 5 4 3 2 1 1 5 4 4 3 2 1 5 4 3 2 1 1 5 4 4 3 2 1 5 4
[39] 3 3 2 1 5 4 3 2 2 1 5 5 4 3 2 1 5 4 3 2 2 1 5 4 3 3 2 1 5 4 3 3 2 1 5 4 4 3
[77] 2 1 5 5 4 3 2 1
Levels: 5 < 4 < 3 < 2 < 1
$`1`
[1] -10  -9  -8  -7  -6  -5  -4

$`2`
[1] -3 -2 -1  0  0  0  0

$`3`
[1] 1 2 3 0 0 0 0

$`4`
[1]  4  5  6  7  8  9 10

Warning message:
In ntile(c(-10:10, rep(0, 7)), 4) : common values across groups: 2, 3

BurStMisc documentation built on May 2, 2019, 12:12 p.m.

Related to ntile in BurStMisc...