Skip to content

Commit

Permalink
simplified gwColMeans
Browse files Browse the repository at this point in the history
  • Loading branch information
mayer79 committed Oct 30, 2023
1 parent 2bbbc88 commit 590a001
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions R/utils_calculate.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ wcolMeans <- function(x, w = NULL) {
#' with(iris, gwColMeans(Sepal.Width, g = Species, w = Sepal.Length))
gwColMeans <- function(x, g = NULL, w = NULL, reorder = TRUE) {
if (is.null(g)) {
wg <- if (is.null(w)) NROW(x) else sum(w)
M <- rbind(wcolMeans(x, w = w))
return(list(M = M, w = wg))
denom <- if (is.null(w)) NROW(x) else sum(w)
return(list(M = M, w = denom))
}

# Now the interesting case
Expand All @@ -105,10 +105,8 @@ gwColMeans <- function(x, g = NULL, w = NULL, reorder = TRUE) {
x <- x * w # w is correctly recycled over columns
}
num <- rowsum(x, group = g, reorder = reorder)
wg <- rowsum(w, group = g, reorder = reorder)
p <- ncol(num)
denom <- if (p == 1L) wg else matrix(wg, nrow = nrow(num), ncol = p)
list(M = num / denom, w = as.numeric(wg))
denom <- as.numeric(rowsum(w, group = g, reorder = reorder))
list(M = num / denom, w = denom)
}

#' Weighted Mean Centering
Expand Down

0 comments on commit 590a001

Please sign in to comment.