
base::rank
but much faster. And it accepts vectors, lists, data.frames or data.tables as input. In addition to the ties.method
possibilities provided by base::rank
, it also provides ties.method="dense"
. bit64::integer64
type is also supported.
frank(x, ..., na.last=TRUE, ties.method=c("average", "first", "random", "max", "min", "dense"))
frankv(x, cols=seq_along(x), order=1L, na.last=TRUE, ties.method=c("average", "first", "random", "max", "min", "dense"))
NA
s. If TRUE
, missing values in the data are put last; if FALSE
, they are put first; if NA
, they are removed; if "keep"
they are kept with rank NA
. Details
. NROW(x)
(unless na.last = NA
, when missing values are removed). The vector is of integer type unless ties.method = "average"
when it is of double type (irrespective of ties).
data.table
operations, NA
s are considered identical to other NA
s (and NaN
s to other NaN
s), unlike base::rank
. Therefore, for na.last=TRUE
and na.last=FALSE
, NA
s (and NaN
s) are given identical ranks, unlike rank
. frank
is not limited to vectors. It accepts data.tables (and lists and data.frames) as well. It accepts unquoted column names (with names preceded with a -
sign for descending order, even on character vectors), for e.g., frank(DT, a, -b, c, ties.method="first")
where a,b,c
are columns in DT
. The equivalent in frankv
is the order
argument.
In addition to the ties.method
values possible using base's rank
, it also provides another additional argument "dense" which returns the ranks without any gaps in the ranking. See examples.
data.table
, setkey
, setorder
# on vectors
x = c(4, 1, 4, NA, 1, NA, 4)
# NAs are considered identical (unlike base R)
# default is average
frankv(x) # na.last=TRUE
frankv(x, na.last=FALSE)
# ties.method = min
frankv(x, ties.method="min")
# ties.method = dense
frankv(x, ties.method="dense")
# on data.table
DT = data.table(x, y=c(1, 1, 1, 0, NA, 0, 2))
frankv(DT, cols="x") # same as frankv(x) from before
frankv(DT, cols="x", na.last="keep")
frankv(DT, cols="x", ties.method="dense", na.last=NA)
frank(DT, x, ties.method="dense", na.last=NA) # equivalent of above using frank
# on both columns
frankv(DT, ties.method="first", na.last="keep")
frank(DT, ties.method="first", na.last="keep") # equivalent of above using frank
# order argument
frank(DT, x, -y, ties.method="first")
# equivalent of above using frankv
frankv(DT, order=c(1L, -1L), ties.method="first")
Run the code above in your browser using DataLab