-
Notifications
You must be signed in to change notification settings - Fork 0
/
margin.R
32 lines (29 loc) · 872 Bytes
/
margin.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# MARGIN
#' @include AllGenerics.R
NULL
# Margin =======================================================================
#' @export
#' @rdname margin
#' @aliases margin,CompositionMatrix-method
setMethod(
f = "margin",
signature = c("CompositionMatrix"),
definition = function(x, parts = c(1, 2), name = "*") {
## Validation
p <- NCOL(x)
parts <- unique(parts)
if (is.character(parts)) parts <- match(parts, colnames(x))
if (p <= length(parts)) return(x)
if (p == length(parts) + 1) {
d <- seq_len(p)
d <- c(d[parts], d[-parts])
return(x[, d, drop = FALSE])
}
rest <- x[, -parts, drop = FALSE]
star <- apply(X = rest, MARGIN = 1, FUN = gmean)
mar <- cbind(x[, parts, drop = FALSE], star)
colnames(mar) <- c(colnames(x)[parts], name[[1L]])
clo <- closure(mar)
methods::initialize(x, clo)
}
)