-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathweightedMean.c
62 lines (50 loc) · 1.78 KB
/
weightedMean.c
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
/***************************************************************************
Public methods:
SEXP weightedMean(SEXP x, SEXP w, SEXP idxs, SEXP naRm, SEXP refine)
Copyright Henrik Bengtsson, 2014
**************************************************************************/
#include <Rdefines.h>
#include "000.types.h"
#include <R_ext/Error.h>
#include "weightedMean_lowlevel.h"
SEXP weightedMean(SEXP x, SEXP w, SEXP idxs, SEXP naRm, SEXP refine) {
SEXP ans;
int narm, refine2;
double avg = NA_REAL;
R_xlen_t nx, nw;
/* Argument 'x': */
assertArgVector(x, (R_TYPE_LGL | R_TYPE_INT | R_TYPE_REAL), "x");
nx = xlength(x);
/* Argument 'x': */
assertArgVector(w, (R_TYPE_REAL), "w");
nw = xlength(w);
if (nx != nw) {
error("Argument 'x' and 'w' are of different lengths: %lld != %lld", (long long int)nx, (long long int)nw);
}
/* Argument 'naRm': */
narm = asLogicalNoNA(naRm, "na.rm");
/* Argument 'refine': */
refine2 = asLogicalNoNA(refine, "refine");
/* Argument 'idxs': */
R_xlen_t nidxs;
int idxsHasNA;
R_xlen_t *cidxs = validateIndicesCheckNA(idxs, nx, 1, &nidxs, &idxsHasNA);
/* Double matrices are more common to use. */
if (isReal(x)) {
avg = weightedMean_dbl(REAL(x), nx, REAL(w), cidxs, nidxs, idxsHasNA, narm, refine2);
} else if (isInteger(x) | isLogical(x)) {
avg = weightedMean_int(INTEGER(x), nx, REAL(w), cidxs, nidxs, idxsHasNA, narm, refine2);
}
/* Return results */
PROTECT(ans = allocVector(REALSXP, 1));
REAL(ans)[0] = avg;
UNPROTECT(1);
return(ans);
} // weightedMean()
/***************************************************************************
HISTORY:
2015-06-07 [DJ]
o Supported subsetted computation.
2014-12-08 [HB]
o Created.
**************************************************************************/