HenrikBengtsson / matrixStats

R package: Methods that Apply to Rows and Columns of Matrices (and to Vectors)
https://cran.r-project.org/package=matrixStats
203 stars 33 forks source link

count(x, value = value) to support length(value) > 1 #124

Open HenrikBengtsson opened 6 years ago

HenrikBengtsson commented 6 years ago

count(x, value = value) is currently restricted to length(value) = 1. It "shouldn't be too hard" to generalize this to also support length(value) > 1. Alternative name could be table2(x, values) which falls back to table() when values = NULL.

For performance reasons, it could be that length(value) == 1 should remain as it's currently implemented. Maybe it's also worth making length(value) == 2 and == 3 optimized using 2 and 3 internal variables.

Then, we could have:

tableLogical <- function(x) {
  counts <- c(FALSE = 0L, TRUE = 0L, NA = 0L)
  counts[2:3] <- count(x, value = c(TRUE, NA))
  counts[1] <- length(x) - counts[2] - counts[3]
  counts
}

See also https://twitter.com/henrikbengtsson/status/957462928784990209