diff options
author | Suraj Yerramilli | 2016-03-21 12:47:54 +0530 |
---|---|---|
committer | Suraj Yerramilli | 2016-03-21 12:47:54 +0530 |
commit | 9fac5b3b0aa47416d73871fcf13637655ed74d19 (patch) | |
tree | 62e398784b1575dc6b499f8bdc51163c161be33e | |
parent | 23f84e46bd3818ba5f6bf355b83938329dfbb9db (diff) | |
download | SysID-R-code-9fac5b3b0aa47416d73871fcf13637655ed74d19.tar.gz SysID-R-code-9fac5b3b0aa47416d73871fcf13637655ed74d19.tar.bz2 SysID-R-code-9fac5b3b0aa47416d73871fcf13637655ed74d19.zip |
utility function for multiple assignment
-rw-r--r-- | R/util.R | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/R/util.R b/R/util.R new file mode 100644 index 0000000..bb2066e --- /dev/null +++ b/R/util.R @@ -0,0 +1,42 @@ +# Generic form +'%=%' = function(l, r, ...) UseMethod('%=%') + +# Binary Operator +'%=%.lbunch' = function(l, r, ...) { + Envir = as.environment(-1) + + if (length(r) > length(l)) + warning("RHS has more args than LHS. Only first", length(l), "used.") + + if (length(l) > length(r)) { + warning("LHS has more args than RHS. RHS will be repeated.") + r <- extendToMatch(r, l) + } + + for (II in 1:length(l)) { + do.call('<-', list(l[[II]], r[[II]]), envir=Envir) + } +} + +# Used if LHS is larger than RHS +extendToMatch <- function(source, destin) { + s <- length(source) + d <- length(destin) + + # Assume that destin is a length when it is a single number and source is not + if(d==1 && s>1 && !is.null(as.numeric(destin))) + d <- destin + + dif <- d - s + if (dif > 0) { + source <- rep(source, ceiling(d/s))[1:d] + } + return (source) +} + +# Grouping the left hand side +g = function(...) { + List = as.list(substitute(list(...)))[-1L] + class(List) = 'lbunch' + return(List) +}
\ No newline at end of file |