Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meep() - multivariate sweep()? #9

Open
robertschnitman opened this issue Sep 7, 2018 · 2 comments
Open

meep() - multivariate sweep()? #9

robertschnitman opened this issue Sep 7, 2018 · 2 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@robertschnitman
Copy link
Owner

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 robertschnitman self-assigned this Sep 7, 2018
@robertschnitman robertschnitman added enhancement New feature or request help wanted Extra attention is needed labels Sep 7, 2018
@robertschnitman
Copy link
Owner Author

robertschnitman commented Sep 21, 2018

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
Copy link
Owner Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant