From a3b5bece37eb03927827ced973b7498ba41e0171 Mon Sep 17 00:00:00 2001 From: Ale Santuz <7524938+alesantuz@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:00:50 +0100 Subject: [PATCH] Improved robustness of function `classify_kmeans` --- DESCRIPTION | 2 +- NEWS.md | 12 ++++++++++++ R/classify_kmeans.R | 13 +++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7fb2845..c31aab0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: musclesyneRgies Title: Extract Muscle Synergies from Electromyography -Version: 1.2.5.9007 +Version: 1.2.5.9008 Authors@R: person("Alessandro", "Santuz", , "alessandro.santuz@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6577-5101")) diff --git a/NEWS.md b/NEWS.md index e0cb04a..d84faee 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,15 @@ +# musclesyneRgies 1.2.5.9008 (development version) +### How to install +``` +install.packages("remotes") +remotes::install_github("alesantuz/musclesyneRgies") +``` +### How to use +README and vignettes are available both on [CRAN](https://CRAN.R-project.org/package=musclesyneRgies) and on [GitHub](https://github.com/alesantuz/musclesyneRgies). + +### What's changed +- Improved robustness of function `classify_kmeans`, which can now deal with trial names containing dots. + # musclesyneRgies 1.2.5.9007 (development version) ### How to install ``` diff --git a/R/classify_kmeans.R b/R/classify_kmeans.R index a84c4b4..f7880e9 100644 --- a/R/classify_kmeans.R +++ b/R/classify_kmeans.R @@ -106,8 +106,17 @@ classify_kmeans <- function(x, syn0 <- which(!duplicated(trials)) trials <- make.unique(trials) trials[syn0] <- paste0(trials[syn0], "_Syn0") - # Assign incremental Syn number to other names - trials <- gsub("\\.", "_Syn", trials) + # Assign incremental Syn number to other names. + # The following is written like that to consider trial names + # that might contain dots. The Perl-style positive lookahead + # ensures that the dot is followed by zero or more characters + # that are not dots ([^.]), all the way to the end of the string ($). + # The conditional element excludes trials ending with "_Syn0". + trials <- ifelse( + grepl("_Syn0$", trials), + trials, + gsub("\\.(?=[^.]*$)", "_Syn", trials, perl = TRUE) + ) # Start from Syn1 instead that from Syn0 temp1 <- gsub("[0-9]$", "", trials) temp2 <- as.numeric(gsub(".*_Syn", "", trials)) + 1