/
rowscale.R
73 lines (70 loc) · 2.09 KB
/
rowscale.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#' S3 method for row-scaling
#' @param x Any object
#' @param center Logical, whether centering should be done before scaling
#' @param scale Logical, whether scaling should be done
#' @return The input object with rows scaled
#' @export rowscale
rowscale <- function(x, center = TRUE, scale=TRUE) UseMethod("rowscale")
#' Scale a matrix by row
#'
#' Scaling a matrix by row can be slightly slower due to a transposing step.
#'
#' @param x An matrix
#' @param center Logical, passed to \code{scale}.
#' to \code{TRUE}
#' @param scale Logical, passed to \code{scale}.
#' \code{TRUE}
#' @return A matrix with each row scaled.
#' @author Jitao David Zhang <jitao_david.zhang@@roche.com>
#' @seealso \code{\link{scale}}
#' @examples
#'
#' mat <- matrix(rnorm(20), nrow=4)
#' rs.mat <- rowscale(mat)
#'
#' print(mat)
#' print(rs.mat)
#' rowMeans(rs.mat)
#' apply(rs.mat, 1L, sd)
#'
#' rowscale(mat, center=FALSE, scale=FALSE) ## equal to mat
#' rowscale(mat, center=TRUE, scale=FALSE)
#' rowscale(mat, center=FALSE, scale=TRUE)
#'
#' @method rowscale matrix
#' @export
rowscale.matrix <- function(x, center=TRUE, scale=TRUE) {
t(scale(t(x),center=center, scale=scale))
}
#' Scale a table by row
#'
#' Scaling a table by row can be slightly slower due to a transposing step.
#'
#' @param x An matrix
#' @param center Logical, passed to \code{scale}.
#' to \code{TRUE}
#' @param scale Logical, passed to \code{scale}.
#' \code{TRUE}
#' @return A table with each row scaled.
#' @author Jitao David Zhang <jitao_david.zhang@@roche.com>
#' @seealso \code{\link{scale}}
#' @examples
#'
#' letterDf <- data.frame(from=c("A", "A", "B", "C"), to=c("A", "B", "C", "A"))
#' tbl <- table(letterDf$from, letterDf$to)
#' tblRowscale <- rowscale(tbl)
#'
#' print(tbl)
#' print(tblRowscale)
#' rowMeans(tblRowscale)
#' apply(tblRowscale, 1L, sd)
#'
#' rowscale(tbl, center=FALSE, scale=FALSE) ## equal to mat
#' rowscale(tbl, center=TRUE, scale=FALSE)
#' rowscale(tbl, center=FALSE, scale=TRUE)
#'
#' @method rowscale table
#' @export
rowscale.table <- function(x, center=TRUE, scale=TRUE) {
t(scale(t(x),center=center, scale=scale))
}