File: ntile.Rd

package info (click to toggle)
r-cran-dplyr 1.1.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,292 kB
  • sloc: cpp: 1,403; sh: 17; makefile: 7
file content (48 lines) | stat: -rw-r--r-- 1,412 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/rank.R
\name{ntile}
\alias{ntile}
\title{Bucket a numeric vector into \code{n} groups}
\usage{
ntile(x = row_number(), n)
}
\arguments{
\item{x}{A vector to rank

By default, the smallest values will get the smallest ranks. Use \code{\link[=desc]{desc()}}
to reverse the direction so the largest values get the smallest ranks.

Missing values will be given rank \code{NA}. Use \code{coalesce(x, Inf)} or
\code{coalesce(x, -Inf)} if you want to treat them as the largest or smallest
values respectively.

To rank by multiple columns at once, supply a data frame.}

\item{n}{Number of groups to bucket into}
}
\description{
\code{ntile()} is a sort of very rough rank, which breaks the input vector into
\code{n} buckets. If \code{length(x)} is not an integer multiple of \code{n}, the size of
the buckets will differ by up to one, with larger buckets coming first.

Unlike other ranking functions, \code{ntile()} ignores ties: it will create
evenly sized buckets even if the same value of \code{x} ends up in different
buckets.
}
\examples{
x <- c(5, 1, 3, 2, 2, NA)
ntile(x, 2)
ntile(x, 4)

# If the bucket sizes are uneven, the larger buckets come first
ntile(1:8, 3)

# Ties are ignored
ntile(rep(1, 8), 3)
}
\seealso{
Other ranking functions: 
\code{\link{percent_rank}()},
\code{\link{row_number}()}
}
\concept{ranking functions}