Skip to content

Commit

Permalink
Added whichSetOfN() and some cleaning and updating.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsyctc committed Oct 12, 2024
1 parent 8f00346 commit dfe3390
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 5 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
^CODE_OF_CONDUCT.md$
^CONTRIBUTING.md$
^docs$
^doc$
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 <chris@psyctc.org>
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
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,":=")
Expand Down
28 changes: 28 additions & 0 deletions R/correctAttenuation.R
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion R/getCIPearson.R
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions R/getCSC.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -36,6 +37,7 @@
#'
#' @export
#'
#' @importFrom mathjaxr preview_rd
#' @importFrom stats qnorm
#' @importFrom tibble as_tibble
#' @importFrom magrittr %>%
Expand Down
60 changes: 60 additions & 0 deletions R/whichSetofN.R
Original file line number Diff line number Diff line change
@@ -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")
4 changes: 4 additions & 0 deletions man/convertClipboardAuthorNames.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/getCIPearson.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/getCSC.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions man/whichSetOfN.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified tests/testthat/Rplots.pdf
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/testthat/whichSetOfN.R
Original file line number Diff line number Diff line change
@@ -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))
})

0 comments on commit dfe3390

Please sign in to comment.