Skip to content

Commit 0193075

Browse files
committed
A new function, which_top_n(), to return the indices of top or bottom n elements of a vector, with several methods for resolving ties.
fixes #40
1 parent f3b14d1 commit 0193075

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: statnet.common
2-
Version: 4.13.0-504
3-
Date: 2025-11-28
2+
Version: 4.13.0-507
3+
Date: 2025-12-16
44
Title: Common R Scripts and Utilities Used by the Statnet Project Software
55
Authors@R: c(
66
person(c("Pavel", "N."), "Krivitsky", role=c("aut","cre"), email="pavel@statnet.org", comment=c(ORCID="0000-0002-9101-3362", affiliation="University of New South Wales")),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export(unwhich)
167167
export(update_snctrl)
168168
export(var.mcmc.list)
169169
export(vector.namesmatch)
170+
export(which_top_n)
170171
export(xAxT)
171172
export(xTAx)
172173
export(xTAx_eigen)

R/misc.utilities.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,3 +1332,53 @@ enlist <- function(x, test = c("inherits", "vector", "list")) {
13321332

13331333
if (test(x)) x else list(x)
13341334
}
1335+
1336+
#' Top or bottom `n` elements of a vector
1337+
#'
1338+
#' Return the indices of the top or bottom `abs(n)` elements of a
1339+
#' vector, with several methods for resolving ties.
1340+
#'
1341+
#' @param x a vector on which [rank()] can be evaluated.
1342+
#'
1343+
#' @param n the number of elements to attempt to select; if positive
1344+
#' top `n` are selected, and if negative bottom `-n`.
1345+
#'
1346+
#' @param tied a string to specify how to handle multiple elements
1347+
#' tied for `n`'th place: `all` or `none` to include all or none of
1348+
#' the tied elements, returning a longer or shorter vector than `n`,
1349+
#' respectively; `given` (the default) to use the order in which the
1350+
#' elements are found in `x`.
1351+
#'
1352+
#' @return An integer vector of indices on `x`, with an attribute
1353+
#' `attr(, "tied")` with the indicies of the tied elements (possibly
1354+
#' empty).
1355+
#'
1356+
#' @examples
1357+
#'
1358+
#' (x <- rep(1:4, 1:4))
1359+
#'
1360+
#' stopifnot(identical(which_top_n(x, 5, "all"), structure(4:10, tied = 4:6)))
1361+
#' stopifnot(identical(which_top_n(x, 5, "none"), structure(7:10, tied = 4:6)))
1362+
#' stopifnot(identical(which_top_n(x, 5), structure(6:10, tied = 4:6)))
1363+
#'
1364+
#' stopifnot(identical(which_top_n(x, -5, "all"), structure(1:6, tied = 4:6)))
1365+
#' stopifnot(identical(which_top_n(x, -5, "none"), structure(1:3, tied = 4:6)))
1366+
#' stopifnot(identical(which_top_n(x, -5), structure(1:5, tied = 4:6)))
1367+
#'
1368+
#' @export
1369+
which_top_n <- function(x, n, tied = c("given", "all", "none")) {
1370+
tied <- match.arg(tied)
1371+
1372+
ordcut <- if (n > 0) function(r) (length(x) + 1 - r) <= n
1373+
else function(r) r <= -n
1374+
s1 <- ordcut(rank(x, ties.method = "min"))
1375+
s2 <- ordcut(rank(x, ties.method = "max"))
1376+
1377+
structure(
1378+
which(switch(tied,
1379+
given = ordcut(rank(x, ties.method = "first")),
1380+
all = s1 | s2,
1381+
none = s1 & s2)),
1382+
tied = which(s1 != s2)
1383+
)
1384+
}

man/which_top_n.Rd

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)