From ce270f6f990cf688070ba2238ffa1aeb27c967f9 Mon Sep 17 00:00:00 2001 From: Dylan Beaudette Date: Thu, 18 Feb 2021 11:43:02 -0800 Subject: [PATCH] multiple matches (SG only) should be sorted on character index --- R/formative-elements.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/R/formative-elements.R b/R/formative-elements.R index af92d0f..ae8bd0e 100644 --- a/R/formative-elements.R +++ b/R/formative-elements.R @@ -79,6 +79,7 @@ FormativeElements <- function(x, level = c("soilorder","suborder","greatgroup"," grep(y, x = haystack)[1] }) + # keep corresponding definitions defs <- do.call('rbind', lapply(sprintf("^%s$", names(idx)), function(z) subset(lut, grepl(z, lut$element)))) rownames(defs) <- NULL @@ -87,10 +88,16 @@ FormativeElements <- function(x, level = c("soilorder","suborder","greatgroup"," if(level != "subgroup") { cidx <- suppressWarnings(max(loc.start[, 1], na.rm = TRUE)) + } else { cidx <- loc.start[, 1] } + # attempt sorting multiple matches + cidx.order <- order(cidx) + defs <- defs[cidx.order, ] + cidx <- cidx[cidx.order] + if(any(is.nan(cidx))) { cidx[is.nan(cidx)] <- NA }