From dfe3390b245f227883e2ad3110c95c42e0317031 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 12 Oct 2024 14:40:26 +0200 Subject: [PATCH] Added whichSetOfN() and some cleaning and updating. --- .Rbuildignore | 1 + DESCRIPTION | 6 +-- NAMESPACE | 2 + R/correctAttenuation.R | 28 ++++++++++++++ R/getCIPearson.R | 2 +- R/getCSC.R | 2 + R/whichSetofN.R | 60 +++++++++++++++++++++++++++++ man/convertClipboardAuthorNames.Rd | 4 ++ man/getCIPearson.Rd | 2 +- man/getCSC.Rd | 1 + man/whichSetOfN.Rd | 47 ++++++++++++++++++++++ tests/testthat/Rplots.pdf | Bin 14510 -> 14510 bytes tests/testthat/whichSetOfN.R | 22 +++++++++++ 13 files changed, 172 insertions(+), 5 deletions(-) create mode 100755 R/correctAttenuation.R create mode 100755 R/whichSetofN.R create mode 100644 man/whichSetOfN.Rd create mode 100644 tests/testthat/whichSetOfN.R diff --git a/.Rbuildignore b/.Rbuildignore index f6a4542..57bbeef 100755 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -16,3 +16,4 @@ ^CODE_OF_CONDUCT.md$ ^CONTRIBUTING.md$ ^docs$ +^doc$ diff --git a/DESCRIPTION b/DESCRIPTION index 2c03904..4dbc601 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: CECPfuns Type: Package Title: Package of Utility Functions for Psychological Therapies, Mental Health and Well-being Work (Created by Chris Evans and Clara Paz) -Version: 0.0.0.9041 +Version: 0.0.0.9042 Maintainer: Chris Evans Description: This should evolve into a repository of all the functions that I (CE) and Clara Paz (CP) have created (so far only CE!) and tested enough to @@ -26,8 +26,8 @@ License: MIT + file LICENSE Encoding: UTF-8 LazyData: no Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 -RdMacros: mathjaxr +RoxygenNote: 7.3.2 +RdMacros: mathjaxr Imports: mathjaxr, ggplot2, diff --git a/NAMESPACE b/NAMESPACE index 4ea30e0..a736770 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,6 +32,7 @@ export(plotBinconf) export(plotCIPearson) export(plotCIProportion) export(plotQuantileCIsfromDat) +export(whichSetOfN) importFrom(clipr,clipr_available) importFrom(clipr,read_clip) importFrom(clipr,write_clip) @@ -73,6 +74,7 @@ importFrom(ggplot2,theme_update) importFrom(ggplot2,xlab) importFrom(ggplot2,ylab) importFrom(magrittr,"%>%") +importFrom(mathjaxr,preview_rd) importFrom(methods,is) importFrom(readr,read_csv) importFrom(rlang,":=") diff --git a/R/correctAttenuation.R b/R/correctAttenuation.R new file mode 100755 index 0000000..22cc698 --- /dev/null +++ b/R/correctAttenuation.R @@ -0,0 +1,28 @@ +#' Function to check if something is a vector, ignoring attributes +#' +#' @param x object to test +#' @noRd +#' @return logical: TRUE if x is a vector even if it has attributes +#' +#' @importFrom methods is +#' +#' @examples +#' \dontrun{ +#' tmpLetters <- letters +#' is.vector(tmpLetters) +#' comment(tmpLetters) <- "Attached comment" +#' is.vector(tmpLetters) # FALSE because tmpLetters is no longer a simple vector +#' checkIsVector(tmpLetters) # TRUE +#' } +#' +#' @author Chris Evans +#' @section History/development log: +#' Started 12.iv.21 +#' +checkIsVector <- function(x) { + ### alternative to base::is.vector() where you aren't worried about a vector having additional attributes + ### if it does base::is.vector() will return FALSE + methods::is(x, "vector") && !is.list(x) + ### this will return FALSE if x is a list, you may occasionally want to treat a list of length 1 as a vector + ### use checkIsOneDim() for that +} diff --git a/R/getCIPearson.R b/R/getCIPearson.R index d382fe1..98150b7 100644 --- a/R/getCIPearson.R +++ b/R/getCIPearson.R @@ -33,7 +33,7 @@ #' observed correlation really was perfect there is no way given the data to compute a #' sensible lower bound (for observed R = 1.0, upper bound for observed R = -1). Returning #' c(1, 1) for perfect positive correlation and c(-1, -1) for perfect negative correlation -#' is the only sensible option and is in line with cor.test{stats}. +#' is the only sensible option and is in line with stats::cor.test(). #' @examples #' \dontrun{ diff --git a/R/getCSC.R b/R/getCSC.R index 2cf4f95..c915c22 100755 --- a/R/getCSC.R +++ b/R/getCSC.R @@ -27,6 +27,7 @@ #' method c is this. #' #' \loadmathjax{} +#' #' \mjdeqn{\frac{SD_{HS}*M_{notHS} + SD_{notHS}*M_{HS}}{SD_{HS}+SD_{notHS}}}{} #' #' (with SD for Standard Deviation (doh!) and M for Mean) @@ -36,6 +37,7 @@ #' #' @export #' +#' @importFrom mathjaxr preview_rd #' @importFrom stats qnorm #' @importFrom tibble as_tibble #' @importFrom magrittr %>% diff --git a/R/whichSetofN.R b/R/whichSetofN.R new file mode 100755 index 0000000..0b7f5a5 --- /dev/null +++ b/R/whichSetofN.R @@ -0,0 +1,60 @@ +#' Function that takes an index number and gives which sequential set of N it is in +#' @description +#' Finds the sequential number of sets of data when reading fixed size multirow blocks of rows. +#' @param x object to test +#' @param n size of set +#' +#' @return integer: the number of the set, 1,2,3 ... +#' +#' @family utility functions +#' +#' @section Background: +#' I am quite often importing data with a multirow nested structure so I may have data from participants +#' with different ID values and with different occasions per participant and then some fixed number of +#' rows of data per person per occasion. For one set of data I might have say four rows of medication +#' data per participant per occasion per actual medication prescribed. I use whichSetOfN(row_number(), 4) +#' to tell me the sequential number of the prescription this row (found by row_number() inside a group_by()). +#' +#' @export +#' +#' @examples +#' whichSetOfN(1:7, 3) +#' ### shows that 1:3 belong to set 1, 4:6 to set 2 and 7 to set 3 +#' +#' +#' @author Chris Evans +#' @section History/development log: +#' Started 12.x.24 +#' +whichSetOfN <- function(x, n){ + ### sanity checking + if (x[1] <= 0) { + stop("Index number, x, must be 1 or higher") + } + if (abs(x[1] - round(x[1])) > .05) { + warning(paste0("The x value you input: ", + x[1], + " is not an integer, is this really what you want?")) + } + if (n <= 2) { + stop("Set size must be 2 or higher") + } + if (abs(n - round(n)) > .0000005) { + stop(paste0("The n value you input: ", + x, + " is not an integer. I don't believe you meant that, the set size must be an integer.")) + } + + ### OK do the work + if (x == 1){ + return(1) + } + tmpX <- 1 + (x %/% n) + if (x[1] %% n == 0) { + return(tmpX - 1) + } else { + return(tmpX) + } +} + +whichSetOfN <- Vectorize(whichSetOfN, vectorize.args = "x") diff --git a/man/convertClipboardAuthorNames.Rd b/man/convertClipboardAuthorNames.Rd index 9bf8c10..8dcd66d 100644 --- a/man/convertClipboardAuthorNames.Rd +++ b/man/convertClipboardAuthorNames.Rd @@ -73,6 +73,10 @@ convertClipboardAuthorNames(warn = FALSE) convertClipboardAuthorNames(toClipboard = FALSE) } +} +\seealso{ +Other utility functions: +\code{\link{whichSetOfN}()} } \concept{text utility functions} \concept{utility functions} diff --git a/man/getCIPearson.Rd b/man/getCIPearson.Rd index 3724270..ad97151 100644 --- a/man/getCIPearson.Rd +++ b/man/getCIPearson.Rd @@ -46,7 +46,7 @@ ought to reflect that (hence my "something" above!) However, the reality is tha observed correlation really was perfect there is no way given the data to compute a sensible lower bound (for observed R = 1.0, upper bound for observed R = -1). Returning c(1, 1) for perfect positive correlation and c(-1, -1) for perfect negative correlation -is the only sensible option and is in line with cor.test{stats}. +is the only sensible option and is in line with stats::cor.test(). } \section{History/development log}{ diff --git a/man/getCSC.Rd b/man/getCSC.Rd index 5c8ed49..736df58 100755 --- a/man/getCSC.Rd +++ b/man/getCSC.Rd @@ -41,6 +41,7 @@ method c is this. } \loadmathjax{} + \mjdeqn{\frac{SD_{HS}*M_{notHS} + SD_{notHS}*M_{HS}}{SD_{HS}+SD_{notHS}}}{} (with SD for Standard Deviation (doh!) and M for Mean) diff --git a/man/whichSetOfN.Rd b/man/whichSetOfN.Rd new file mode 100644 index 0000000..abca8c1 --- /dev/null +++ b/man/whichSetOfN.Rd @@ -0,0 +1,47 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/whichSetofN.R +\name{whichSetOfN} +\alias{whichSetOfN} +\title{Function that takes an index number and gives which sequential set of N it is in} +\usage{ +whichSetOfN(x, n) +} +\arguments{ +\item{x}{object to test} + +\item{n}{size of set} +} +\value{ +integer: the number of the set, 1,2,3 ... +} +\description{ +Finds the sequential number of sets of data when reading fixed size multirow blocks of rows. +} +\section{Background}{ + +I am quite often importing data with a multirow nested structure so I may have data from participants +with different ID values and with different occasions per participant and then some fixed number of +rows of data per person per occasion. For one set of data I might have say four rows of medication +data per participant per occasion per actual medication prescribed. I use whichSetOfN(row_number(), 4) +to tell me the sequential number of the prescription this row (found by row_number() inside a group_by()). +} + +\section{History/development log}{ + +Started 12.x.24 +} + +\examples{ +whichSetOfN(1:7, 3) +### shows that 1:3 belong to set 1, 4:6 to set 2 and 7 to set 3 + + +} +\seealso{ +Other utility functions: +\code{\link{convertClipboardAuthorNames}()} +} +\author{ +Chris Evans +} +\concept{utility functions} diff --git a/tests/testthat/Rplots.pdf b/tests/testthat/Rplots.pdf index 954ab53c3ebc1f21c52945a85ed4dedf50723e79..48f4586fcb4f2e1fa38330202c6e8fcde8c1da5a 100644 GIT binary patch delta 54 zcmZ2ixUO)5shXjIp^>49v6-=vCYQc%eu_(CNveW|iVzeHk;l?f%3jn22 B4v7E& delta 54 zcmZ2ixUO)5shWYQfr+7|skw=nCYQc%eu_(CNveW|iVzeHk!Nx8X3jnDu B4y*tG diff --git a/tests/testthat/whichSetOfN.R b/tests/testthat/whichSetOfN.R new file mode 100644 index 0000000..cbc9b9d --- /dev/null +++ b/tests/testthat/whichSetOfN.R @@ -0,0 +1,22 @@ +testthat::test_that("sanity checks work", { + testthat::expect_error(whichSetOfN(0, 3)) + testthat::expect_error(whichSetOfN(-1:3, 3)) + testthat::expect_error(whichSetOfN(3, -3)) + testthat::expect_error(whichSetOfN(3, 0)) + testthat::expect_error(whichSetOfN(3, 1)) + testthat::expect_error(whichSetOfN(3, 2.1)) +}) + +## test warnings +testthat::test_that("sanity checks work", { + testthat::expect_warning(whichSetOfN(.5, 10)) + testthat::expect_warning(whichSetOfN(0.2, 3)) +}) + +### test of outputs +testthat::test_that("Output correct", { + set.seed(12345) + testthat::expect_equal(whichSetOfN(1:30, 9), + c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 4, 4, 4)) +})