socialresearchcentre / projectable

Produce table-like objects made up of special metadata-rich column vectors, and project them into two dimensions.
https://socialresearchcentre.github.io/projectable
GNU General Public License v3.0
3 stars 0 forks source link

Allow for summarised/unsummarised inputs in `col`s #6

Closed kinto-b closed 4 years ago

kinto-b commented 4 years ago

From comment in #1: allow for summarised/unsummarised inputs in cols

Something like this:

col_freq <- function(n = double(), N = double(), p = NULL, summarised = FALSE) {
  if (!summarised) {
    n <- sum(n, na.rm = TRUE)
    N <- sum(N, na.rm = TRUE)
    if (!is.null(p)) stop("May only provide `p` if `summarised` is TRUE")
  }
  if (is.null(p)) p <- n / N
  out <- vctrs::vec_cast_common(n, N, p, .to = double())
  out <- vctrs::vec_recycle_common(!!!out)

  validate_col_freq(
    new_col_freq(out[[1]], out[[2]], out[[3]])
  )
}

So that we can write either

col_freq(mtcars$vs %in% 1, mtcars$vs %in% 0:1)

# OR

col_freq(sum(mtcars$vs %in% 1), sum(mtcars$vs %in% 0:1), summarised = TRUE)

Note: this would make encol_* redundant