-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added whichSetOfN() and some cleaning and updating.
- Loading branch information
Showing
13 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ | |
^CODE_OF_CONDUCT.md$ | ||
^CONTRIBUTING.md$ | ||
^docs$ | ||
^doc$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) |