Skip to content

Commit ef0ffa0

Browse files
Merge pull request #590 from ESCRI11/v6.3.3.-dev-panelaggregration
fix: port panelaggragation fct
2 parents d204aaf + d7382a5 commit ef0ffa0

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

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)