diff --git a/DESCRIPTION b/DESCRIPTION index f33114e5..d27634c1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: RstoxData -Version: 2.2.1-9007 -Date: 2026-06-13 +Version: 2.2.1-9008 +Date: 2026-06-22 Title: Tools to Read and Manipulate Fisheries Data Authors@R: c( person(given = "Arne Johannes", diff --git a/NEWS.md b/NEWS.md index 37e2bbf4..107f8724 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ +# RstoxData v2.2.1-9008 (2026-06-22) +* Added the option colsToSplit in setorderv_numeric(), to indicate which columns to allow splitting by the characters specified by the parameter split and numeric sorting applied to the parts of the split string. +* Added the option doNotSplit in createOrderKey(). + + # RstoxData v2.2.1-9007 (2026-06-13) -* Added support for reading zipped ICESAcoustic XML files (in getIcesVocabulary()). +* Added support for reading zipped ICESAcoustic XML files (change in getIcesVocabulary()). * Removed warning occurring when translation table and variable is coerced to numeric in matchVariable(). diff --git a/R/Utilities.R b/R/Utilities.R index ad579f8b..1e054775 100644 --- a/R/Utilities.R +++ b/R/Utilities.R @@ -675,10 +675,11 @@ orderRowsByKeys <- function(data) { #' @param by Order by the given columns. #' @param key If given and \code{by} is empty, order by the columns with names ending with \code{key}. #' @param split Character: A vector of single character to split by. The default c("-", "/") splits between StoX keys and within StoX keys. +#' @param colsToSplit Character: A vector of the columns to apply splitting in in \code{createOrderKey}, or NA (default) to potentially split all columns specified by \code{by}. #' #' @export #' -setorderv_numeric <- function(dataOne, by = NULL, key = NULL, split = "/") { +setorderv_numeric <- function(dataOne, by = NULL, key = NULL, split = "/", colsToSplit = NA) { #setorderv_numeric <- function(dataOne, by = NULL, key = NULL, ...) { # Locate keys: @@ -698,7 +699,20 @@ setorderv_numeric <- function(dataOne, by = NULL, key = NULL, split = "/") { orderKeys <- paste0(by, "OrderedAfterSplitting") # Create keys which are converted to ranks, splitting first and then treating individual elements as numbers if possible: - dataOne[, (orderKeys) := lapply(.SD, createOrderKey, split = split), .SDcols = by] + if(length(colsToSplit) == 1 && is.na(colsToSplit)) { + doNotSplit <- FALSE + } + else { + doNotSplit <- !by %in% colsToSplit + } + + dataOne[, (orderKeys) := mapply( + createOrderKey, + .SD, + doNotSplit = doNotSplit, + MoreArgs = list(split = split), + SIMPLIFY = FALSE), + .SDcols = by] # Order the rows: # 2024-10-28: Change to sort out sorting: @@ -708,6 +722,7 @@ setorderv_numeric <- function(dataOne, by = NULL, key = NULL, split = "/") { dataOne[, (orderKeys) := NULL] } + return(dataOne) } @@ -721,19 +736,22 @@ addNAs <- function(x, areNAs) { return(x) } -#' Convert a vector to an integer vector where individual string elements at interpreted as numeric if possible. +#' Convert a vector to an integer vector where individual string elements are interpreted as numeric if possible. #' #' @param x A vector. #' @param split A character to split strings by. +#' @param doNotSplit Logical: If TRUE to not split the input to treat the parts as potential numeric values, but merely treat the input as numeri if possible. #' #' @export #' -createOrderKey <- function(x, split = "/") { +createOrderKey <- function(x, split = "/", doNotSplit = FALSE) { - # Split the keys: + # If not character, return the input unchanged:: if(!is.character(x)) { return(x) } + + # Get the first non-NA value, by first checking the first value, and then finding the first non-NA if the first is NA: firstNonNA <- x[1] if(is.na(firstNonNA)) { if(all(is.na(x))) { @@ -741,6 +759,7 @@ createOrderKey <- function(x, split = "/") { } firstNonNA <- x[min(which(!is.na(x)))] } + # If the first element is coercible to numeric, try converting the entire vector to numeric, and check that no NAs were generated: if(!is.na(suppressWarnings(as.numeric(x[1])))) { numberOfNAs <- sum(is.na(x)) @@ -753,6 +772,10 @@ createOrderKey <- function(x, split = "/") { } } + # Split x and treat individual strings as numeric, but not if doNotSplit is TRUE: + if(doNotSplit) { + return(x) + } # Split by the 'split' argument: if(!any(sapply(split, grepl, firstNonNA))) { return(x) @@ -763,6 +786,7 @@ createOrderKey <- function(x, split = "/") { for(thisSplit in split) { splitted <- lapply(splitted, function(x) unlist(strsplit(x, thisSplit, fixed = TRUE))) } + #splitted <- strsplit(x, split, fixed = TRUE) # Check that all have the same number of elements, that is the same number of splits: diff --git a/inst/extdata/functionArguments.rds b/inst/extdata/functionArguments.rds index 9649306b..daef8df8 100644 Binary files a/inst/extdata/functionArguments.rds and b/inst/extdata/functionArguments.rds differ diff --git a/man/createOrderKey.Rd b/man/createOrderKey.Rd index 86fe0ae3..9850b696 100644 --- a/man/createOrderKey.Rd +++ b/man/createOrderKey.Rd @@ -2,15 +2,17 @@ % Please edit documentation in R/Utilities.R \name{createOrderKey} \alias{createOrderKey} -\title{Convert a vector to an integer vector where individual string elements at interpreted as numeric if possible.} +\title{Convert a vector to an integer vector where individual string elements are interpreted as numeric if possible.} \usage{ -createOrderKey(x, split = "/") +createOrderKey(x, split = "/", doNotSplit = FALSE) } \arguments{ \item{x}{A vector.} \item{split}{A character to split strings by.} + +\item{doNotSplit}{Logical: If TRUE to not split the input to treat the parts as potential numeric values, but merely treat the input as numeri if possible.} } \description{ -Convert a vector to an integer vector where individual string elements at interpreted as numeric if possible. +Convert a vector to an integer vector where individual string elements are interpreted as numeric if possible. } diff --git a/man/setorderv_numeric.Rd b/man/setorderv_numeric.Rd index 6e314455..124f78f9 100644 --- a/man/setorderv_numeric.Rd +++ b/man/setorderv_numeric.Rd @@ -4,7 +4,13 @@ \alias{setorderv_numeric} \title{Order a data.table (by reference) by interpreting characters as numeric if possible} \usage{ -setorderv_numeric(dataOne, by = NULL, key = NULL, split = "/") +setorderv_numeric( + dataOne, + by = NULL, + key = NULL, + split = "/", + colsToSplit = NA +) } \arguments{ \item{dataOne}{A data.table.} @@ -14,6 +20,8 @@ setorderv_numeric(dataOne, by = NULL, key = NULL, split = "/") \item{key}{If given and \code{by} is empty, order by the columns with names ending with \code{key}.} \item{split}{Character: A vector of single character to split by. The default c("-", "/") splits between StoX keys and within StoX keys.} + +\item{colsToSplit}{Character: A vector of the columns to apply splitting in in \code{createOrderKey}, or NA (default) to potentially split all columns specified by \code{by}.} } \description{ Order a data.table (by reference) by interpreting characters as numeric if possible