From 8a3f8054fcfb5cef649b3ca4fb2dc7db3d565c28 Mon Sep 17 00:00:00 2001 From: Simon Garnier Date: Sat, 13 Jul 2024 17:48:14 +0200 Subject: [PATCH] Sampling doesn't have to be random --- R/gravitree.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/R/gravitree.R b/R/gravitree.R index 33ea482..b6d72a6 100644 --- a/R/gravitree.R +++ b/R/gravitree.R @@ -1,5 +1,5 @@ #' @export -gravitree <- function(x, m, k = NULL, sample = 1, na_rm = FALSE) { +gravitree <- function(x, m, k = NULL, sample = 1, random = TRUE, na_rm = FALSE) { if (!is.matrix(x)) { x <- as.matrix(x) } @@ -25,7 +25,12 @@ gravitree <- function(x, m, k = NULL, sample = 1, na_rm = FALSE) { } if (sample < 1) { - ix <- sample(1:nr, nr * sample, prob = m) + if (random) { + ix <- sample(1:nr, nr * sample, prob = m) + } else { + ix <- order(m, decreasing = TRUE)[1:(nr * sample)] + } + notix <- which(!(1:nr %in% ix)) xx <- x[ix, , drop = FALSE] mm <- m[ix, , drop = FALSE]