Skip to content

Commit

Permalink
Revert "Add support for using colTabulates() and rowTabulates() w…
Browse files Browse the repository at this point in the history
…ith doubles [#274]"

This reverts commit 17a5ed3.
  • Loading branch information
HenrikBengtsson committed Jan 7, 2025
1 parent 17a5ed3 commit e6d29cd
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 92 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: matrixStats
Version: 1.4.1-9014
Version: 1.4.1-9013
Depends:
R (>= 3.4.0)
Suggests:
Expand Down
11 changes: 0 additions & 11 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@
release of R will have stricter C header requirements that are not
backward compatible with older versions of R.

## New Features

* Add support for using `colTabulates()` and `rowTabulates()` with
doubles in two special cases. The default is that passing doubles
is a mistake, as real values cannot be tabulated. The exception is
when there is a well-defined set of real values, which is the case
when argument `values` is specified and non-`NULL`. Another case is
when the input are real-valued ranks, which are either perfect
integers or 1/2 values, as may arrise from using rank functions
with `ties.method = "average"`.

## Deprecated and Defunct

* The hidden R options for deescalating the error for using `useNames
Expand Down
44 changes: 3 additions & 41 deletions R/rowTabulates.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
#'
#' @inheritParams rowAlls
#'
#' @param x A \code{\link[base]{numeric}}, a \code{\link[base]{logical}}, or
#' @param x An \code{\link[base]{integer}}, a \code{\link[base]{logical}}, or
#' a \code{\link[base]{raw}} NxK \code{\link[base]{matrix}}.
#' Matrix `x` may only be of type \code{\link[base]{double}} if `values`
#' is specified and non-`NULL`, or if the values in `x` are all perfect
#' integers or 1/2 values. The latter allows for tabulating ranks that
#' may arrise from `ties.method = "average"`.
#'
#' @param values An \code{\link[base]{vector}} of J values of count. If
#' \code{\link[base]{NULL}}, all (unique) values are counted.
Expand Down Expand Up @@ -40,9 +36,8 @@ rowTabulates <- function(x, rows = NULL, cols = NULL, values = NULL, ..., useNam
if (is.integer(x)) {
} else if (is.logical(x)) {
} else if (is.raw(x)) {
} else if (is.double(x)) {
} else {
stop(sprintf("Argument '%s' is not integer, double, logical, or raw: %s", "x", storage.mode(x)))
stop(sprintf("Argument '%s' is not integer, logical, or raw: %s", "x", storage.mode(x)))
}

# Apply subset
Expand All @@ -66,22 +61,6 @@ rowTabulates <- function(x, rows = NULL, cols = NULL, values = NULL, ..., useNam
values <- sort.int(values, na.last = TRUE)
names <- as.character(values)
}

## When `values` is not specified, allow only for double `x` that
## contains perfect integers or 1/2 values. This makes it possible
## to tabulate ranks with ties.method = "average".
if (is.double(values)) {
## Extract decimal part of values
remainders <- values - floor(values)
remainders <- remainders[remainders > 0]
if (length(remainders) > 0) {
## Assert that they are all 1/2 values
if (any(remainders != 1/2)) {
stop(sprintf("Argument '%s' must not contain doubles when values = NULL, unless they are perfect integers or 1/2 values", "x"))
}
}
rm(list = "remainders")
}
} else {
if (is.raw(values)) {
names <- sprintf("%x", as.integer(values))
Expand Down Expand Up @@ -122,9 +101,8 @@ colTabulates <- function(x, rows = NULL, cols = NULL, values = NULL, ..., useNam
if (is.integer(x)) {
} else if (is.logical(x)) {
} else if (is.raw(x)) {
} else if (is.double(x)) {
} else {
stop(sprintf("Argument '%s' is not integer, double, logical, or raw: %s", "x", storage.mode(x)))
stop(sprintf("Argument '%s' is not integer, logical, or raw: %s", "x", storage.mode(x)))
}

# Apply subset
Expand All @@ -148,22 +126,6 @@ colTabulates <- function(x, rows = NULL, cols = NULL, values = NULL, ..., useNam
values <- sort.int(values, na.last = TRUE)
names <- as.character(values)
}

## When `values` is not specified, allow only for double `x` that
## contains perfect integers or 1/2 values. This makes it possible
## to tabulate ranks with ties.method = "average".
if (is.double(values)) {
## Extract decimal part of values
remainders <- values - floor(values)
remainders <- remainders[remainders > 0]
if (length(remainders) > 0) {
## Assert that they are all 1/2 values
if (any(remainders != 1/2)) {
stop(sprintf("Argument '%s' must not contain doubles when values = NULL, unless they are perfect integers or 1/2 values", "x"))
}
}
rm(list = "remainders")
}
} else {
if (is.raw(values)) {
names <- sprintf("%x", as.integer(values))
Expand Down
8 changes: 2 additions & 6 deletions man/rowTabulates.Rd

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

33 changes: 0 additions & 33 deletions tests/rowTabulates.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,36 +118,3 @@ for (mode in modes) {

cat(sprintf("Mode: %s...done\n", mode))
} # for (mode ...)


message("Allow for 'x' of type double only for some special case ...")

message(" - Allow for double, i.e. real values, when 'values' is specified")
X <- matrix(rnorm(n = 12L), nrow = 3L)
print(X)
T <- rowTabulates(X, values = X[1:3])
print(T)
T <- colTabulates(X, values = X[1:3])
print(T)

message(" - Allow for double, i.e. real values, when they are ranks")
X <- matrix(c(1, 1, 1, 1, 4, 3, 2, 4, 2, 1, 2, 2), nrow = 3L)
R <- rowRanks(X, ties.method = "average")
print(R)
T <- rowTabulates(R)
print(T)
stopifnot(any(grepl("[.]5$", colnames(T))))
T <- colTabulates(R)
print(T)
stopifnot(any(grepl("[.]5$", colnames(T))))

message(" - In all other cases, passing real values is an error")
X <- matrix(rnorm(n = 12L), nrow = 3L)
res <- tryCatch(colTabulates(X), error = identity)
print(res)
stopifnot(inherits(res, "error"))
res <- tryCatch(rowTabulates(X), error = identity)
print(res)
stopifnot(inherits(res, "error"))

message("Allow for 'x' of type double only for some special case ... done")

0 comments on commit e6d29cd

Please sign in to comment.