From 9eb926eaa37d45feb55a4c8749d6225967d94315 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Sep 2026 07:05:47 +0000 Subject: [PATCH] Fix getCategories() always returning an empty vector getCategories(x) computed x[names(unique(x))], but base R's unique() unconditionally strips names from atomic vectors - confirmed empirically (names(unique(c(a=1,b=2))) is NULL). So names(unique(x)) is always NULL, and indexing x[NULL] always returns an empty vector, regardless of input. This broke the function 100% of the time, including its own documented example (getCategories(c("A"=1,"B"=1,"C"=2,3)) returned named numeric(0) instead of the intended "extract first occurrence of each unique value, keeping its name" result. Fix: use named_categ_vec[!duplicated(named_categ_vec)], matching the already-correct sibling function unique.wNames() elsewhere in this file, which uses the identical duplicated()-based pattern. Unlike unique(), plain logical/numeric subsetting preserves names, and !duplicated() selects first-occurrence positions directly rather than round-tripping through unique()'s (name-losing) values. Verified: the function's own documented example now returns the correct c(A=1, C=2) result; also verified distinct-but-unnamed input values (e.g. two different unnamed entries) are correctly kept as separate entries rather than colliding, since this fix indexes by position, not by the "" empty-string name that a naive names(unique(x))-based fix would still collide on. Version bumped 2.8.16 -> 2.8.18 (distinct from sibling PR #75, which also branches from the same 2.8.16 base and already claimed 2.8.17). --- DESCRIPTION | 2 +- Development/config.R | 2 +- R/CodeAndRoll2.R | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 227f9ee..51ee9ed 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: CodeAndRoll2 Title: CodeAndRoll2 for vector, matrix and list manipulations -Version: 2.8.16 +Version: 2.8.18 Authors@R: person("Abel", "Vertesy", , "av@imba.oeaw.ac.at", role = c("aut", "cre")) Description: CodeAndRoll2 is a set of more than 170 productivity functions diff --git a/Development/config.R b/Development/config.R index cc4bdcb..6712e01 100644 --- a/Development/config.R +++ b/Development/config.R @@ -3,7 +3,7 @@ DESCRIPTION <- list( package.name = "CodeAndRoll2", - version = "2.8.16", + version = "2.8.18", title = "CodeAndRoll2 for vector, matrix and list manipulations", description = "CodeAndRoll2 is a set of more than 170 productivity functions for vector, matrix and list manipulations and math. Used by MarkdownReports, ggExpress, SeuratUtils, etc.", diff --git a/R/CodeAndRoll2.R b/R/CodeAndRoll2.R index efafc51..347c850 100644 --- a/R/CodeAndRoll2.R +++ b/R/CodeAndRoll2.R @@ -834,7 +834,7 @@ table_decreasing_hybrid <- function(vec, first_elements = FALSE, useNA = "ifany" #' @export #' @examples getCategories(c("A" = 1, "B" = 1, "C" = 2, 3)) getCategories <- function(named_categ_vec) { - named_categ_vec[names(unique(named_categ_vec))] + named_categ_vec[!duplicated(named_categ_vec)] }