Skip to content

Commit

Permalink
Export position_name
Browse files Browse the repository at this point in the history
  • Loading branch information
llrs committed May 20, 2024
1 parent 870e3c8 commit 49b1620
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 12 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export(follow_up2)
export(inspect)
export(optimum_batches)
export(optimum_subset)
export(position_name)
export(qcSubset)
export(replicates)
export(sizes_batches)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

* Now it is possible to remove full rows or columns from `spatial()`: like `remove_positions = "A"` (#52).

* Fix a bug in `spatial()` that in some cases assigned multiple samples to the same position (#51).

* Spatial indexes are returned in row, column order: A1, A2, A3, ... A10, B1,.

* Function `position_name()` is now exported to facilitate generating designs.

# experDesign 0.3.0

* Fixed a bug in `spatial()` where multiple samples could be assigned to the
Expand Down
3 changes: 2 additions & 1 deletion R/evaluate_category.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ evaluate_independence <- function(i, pheno) {
#' @param pheno Data.frame with the variables of each sample, one row one sample.
#' @param omit Character vector with the names of the columns to omit.
#' @param na.omit Check the effects of missing values too.
#' @return A logical value indicating if everything is alright (TRUE) or not (FALSE).
#' @return A logical value indicating if everything is alright (`TRUE` or not (`FALSE`).
#' @seealso [valid_followup()].
#' @export
#' @examples
#' rdata <- expand.grid(sex = c("M", "F"), class = c("lower", "median", "high"))
Expand Down
2 changes: 1 addition & 1 deletion R/follow_up.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' data(survey, package = "MASS")
#' survey1 <- survey[1:118, ]
#' survey2 <- survey[119:nrow(survey), ]
#' fu <- follow_up(survey1, survey2, size_subset = 50, iterations = 10)
#' folu <- follow_up(survey1, survey2, size_subset = 50, iterations = 10)
follow_up <- function(original, follow_up, size_subset, omit = NULL,
old_new = "batch", iterations = 500) {
stopifnot(is.character(old_new) & length(old_new) == 1)
Expand Down
14 changes: 14 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,21 @@ translate_index <- function(index,
index
}


#' Create position names for a grid.
#'
#' @param rows Names of the rows.
#' @param columns Names of the columns.
#'
#' @return A data.frame with the rows and columns and the resulting name row+column.
#' The name column is a factor for easier sorting in row, column order.
#' @export
#' @examples
#' position_name(c("A", "B"), 1:2)
position_name <- function(rows, columns) {
if (length(intersect(rows, columns)) >= 1) {
stop("rows and columns shouldn't share names.")
}
positions <- expand.grid(rows, columns, stringsAsFactors = FALSE)
colnames(positions)[1:2] <- c("row", "column")
positions <- positions[order(positions$row, positions$column), ]
Expand Down
6 changes: 4 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ knitr::opts_chunk$set(

<!-- badges: end -->

The goal of experDesign is to help you manage your samples before an experiment but after they are collected.
For example, checking for common problems in the data, and help reduce or even prevent batch bias before performing an experiment, or measure once it is already done
The goal of experDesign is to help you distribute your samples before an experiment but after they are collected.
For example, checking for common problems in the data, and reducing or even preventing batch bias before performing an experiment, or measuring it once the experiment is performed.
It provides four main functions:

* `check_data()`: Check if there are any problems with the data.
* `design()`: Randomize the samples according to their variables.
* `replicates()`: Selects some samples for replicates and randomizes the samples (highly recommended).
* `spatial()`: Randomize the samples on a spatial grid.

There are other helpers.

## Installation

To install the latest version on [CRAN](https://CRAN.R-project.org/package=experDesign) use:
Expand Down
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.re

<!-- badges: end -->

The goal of experDesign is to help you manage your samples before an
The goal of experDesign is to help you distribute your samples before an
experiment but after they are collected. For example, checking for
common problems in the data, and help reduce or even prevent batch bias
before performing an experiment, or measure once it is already done It
provides four main functions:
common problems in the data, and reducing or even preventing batch bias
before performing an experiment, or measuring it once the experiment is
performed. It provides four main functions:

- `check_data()`: Check if there are any problems with the data.
- `design()`: Randomize the samples according to their variables.
- `replicates()`: Selects some samples for replicates and randomizes the
samples (highly recommended).
- `spatial()`: Randomize the samples on a spatial grid.

There are other helpers.

## Installation

To install the latest version on
Expand Down Expand Up @@ -118,6 +120,9 @@ combination with the original according to multiple statistics.
omit <- c("Wr.Hnd", "NW.Hnd", "Fold", "Pulse", "Clap", "Exer", "Height", "M.I")
(keep <- colnames(survey)[!colnames(survey) %in% omit])
#> [1] "Sex" "W.Hnd" "Smoke" "Age"
```

``` r
head(survey[, keep])
#> Sex W.Hnd Smoke Age
#> 1 Female Right Never 18.250
Expand All @@ -126,12 +131,18 @@ head(survey[, keep])
#> 4 Male Right Never 20.333
#> 5 Male Right Never 23.667
#> 6 Female Right Never 21.000
```

``` r

# Set a seed for reproducibility
set.seed(87732135)
# Looking for groups at most of 70 samples.
index <- design(pheno = survey, size_subset = 70, omit = omit, iterations = 100)
#> Warning: There might be some problems with the data use check_data().
```

``` r
index
#> $SubSet1
#> [1] 3 9 10 14 16 21 23 24 25 30 44 46 56 57 59 62 63 68 69
Expand Down Expand Up @@ -164,6 +175,9 @@ a colleague with:
``` r
head(batch_names(index))
#> [1] "SubSet3" "SubSet3" "SubSet1" "SubSet4" "SubSet2" "SubSet2"
```

``` r
# Or via inspect() to keep it in a matrix format:
head(inspect(index, survey[, keep]))
#> Sex W.Hnd Smoke Age batch
Expand Down Expand Up @@ -203,7 +217,9 @@ Two packages allow to distribute the samples on batches:

If you are still designing the experiment and do not have collected any
data [DeclareDesign](https://cran.r-project.org/package=DeclareDesign)
might be relevant for you.
might be relevant for you. But specially the
[randomizr](https://cran.r-project.org/package=randomizr) packages which
makes common forms of random assignment and sampling.

Question in
[Bioinformatics.SE](https://bioinformatics.stackexchange.com/q/4765/48)
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ reference:
- valid_followup
- title: "Other"
- contents:
- position_name
- create_subset
- compare_index
- entropy
Expand Down
2 changes: 1 addition & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
This updates adds some functions and fixes a bug found by a user.
This updates adds some functions and fixes a bug.
Checks passed without notes locally in some other machines there was a note about examples above 5s.
5 changes: 4 additions & 1 deletion man/check_data.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/follow_up.Rd

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

23 changes: 23 additions & 0 deletions man/position_name.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test-spatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ test_that("Removing columns works", {
expect_lte(length(index2), 9*12)
expect_true(all(names(index2) %in% position_name(rows = LETTERS[1:9], 1:12)$name))
expect_false(any(table(batch_names(index), batch_names(index2)) > 1))
})

test_that("position_name works", {
expect_error(position_name(1:2, 1:2))
pn <- position_name(LETTERS[1:2], 1:2)
expect_s3_class(pn, "data.frame")
expect_equal(pn$row, rep(LETTERS[1:2], each = 2))
expect_equal(pn$column, rep(1:2, length.out = 4))
expect_equal(pn$name, as.factor(paste0(rep(LETTERS[1:2], each = 2), rep(1:2, length.out = 4))))
})

0 comments on commit 49b1620

Please sign in to comment.