robertschnitman / afp

Applied Functional Programming - Tools to simplify iterative processes.
https://afp.netlify.com
GNU General Public License v3.0
0 stars 0 forks source link

meep() - multivariate sweep()? #9

Open robertschnitman opened 6 years ago

robertschnitman commented 6 years ago

The function sweep() is underrated. So, to expand upon its benefits, how should a multivariate version of sweep() resemble?

# Rough draft
meep <- function(f, s, x, ..., keep.rownames = FALSE) { 
# function, summary statistic, dataset, mapply inputs, rownames condition

  output <- mapply(function(y) f(y, s(y)), x, ...)

  cond_len <- length(NROW(x)) == length(NROW(output))

  if (length(dim(x)) > 1) {

    if (keep.rownames == TRUE & cond_len == FALSE) {

      stop('length(NROW(x)) != length(NROW(output))')

    } else if (keep.rownames == TRUE & cond_len == TRUE) {

      rownames(output) <- rownames(x)

    }

  }

  output

}

# Example
meep(`/`, mean, mtcars, keep.rownames = TRUE)
robertschnitman commented 6 years ago

To note, we can replicate the example with sweep() and sapply():

sweep(mtcars, 2, sapply(mtcars, mean), `/`)

09/20/2018 EDIT: Thus,

# f = primary function, most likely a binary operator.
# s = summary statistic function
meep <- function(f, s, x, m) sweep(x, m, apply(x, m, s), f)
# Worth noting that ?sweep has an example with apply().

However, the above draft does not solve the multivariate case. Is one even necessary? The point of sweep() is to "sweep out" (i.e. map a function to) the rows or columns by the associated summary statistic--there is not a tertiary consideration. Perhaps the benefit of meep() is the convenience of apply() being written within the function? This benefit comes at the cost of being unable to input a list of non-function elements for "s" as with sweep().

Use sweep()/apply() combination and rename to "brush" or "mop"--because we are applying the same process to each row or column? [09/21/2018 EDIT: grammar].

robertschnitman commented 6 years ago

Based on the previous comment, mop.r has been created. This issue will be left open in case an idea for a true multivariate sweep propagates.