Skip to content

Commit 0734222

Browse files
Merge branch 'datashield:v6.3.3-dev' into v6.3.3-dev
2 parents d43b1c5 + ef0ffa0 commit 0734222

4 files changed

Lines changed: 55 additions & 1 deletion

File tree

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ Imports:
4949
ggplot2,
5050
gridExtra,
5151
data.table,
52-
panelaggregation,
5352
methods,
5453
dplyr
5554
Suggests:

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(computeWeightedMeans)
34
export(ds.Boole)
45
export(ds.abs)
56
export(ds.asCharacter)
@@ -119,3 +120,7 @@ export(ds.var)
119120
export(ds.vectorCalc)
120121
import(DSI)
121122
import(data.table)
123+
importFrom(stats,as.formula)
124+
importFrom(stats,na.omit)
125+
importFrom(stats,ts)
126+
importFrom(stats,weighted.mean)

R/computeWeightedMeans.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#' Compute Weighted Mean by Group
2+
#'
3+
#' This function is originally from the panelaggregation package.
4+
#' It has been ported here in order to bypass the package being
5+
#' kicked off CRAN.
6+
#'
7+
#' @author Matthias Bannert, Gabriel Bucur
8+
#' @param data_table a data.table
9+
#' @param variables character name of the variable(s) to focus on. The variables must be in the data.table
10+
#' @param weight character name of the data.table column that contains a weight.
11+
#' @param by character vector of the columns to group by
12+
#' @import data.table
13+
#' @importFrom stats as.formula na.omit ts weighted.mean
14+
#' @export
15+
computeWeightedMeans <- function(data_table, variables, weight, by) {
16+
17+
if (is.null(weight)) {
18+
res_dt <- data_table[, lapply(.SD, mean, na.rm = TRUE), .SDcols = variables, by = by]
19+
} else {
20+
res_dt <- data_table[, lapply(.SD, weighted.mean, weight = eval(as.name(weight)), na.rm = TRUE),
21+
.SDcols = variables, by = by]
22+
}
23+
24+
res_dt
25+
}

man/computeWeightedMeans.Rd

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)