@@ -26,15 +26,14 @@ namespace Rcpp{
26
26
namespace sugar {
27
27
28
28
template <int RTYPE, bool NA, typename T>
29
- class Mean : public Lazy <typename Rcpp::traits::storage_type<RTYPE>::type , Mean<RTYPE,NA,T> > {
29
+ class Mean : public Lazy <double , Mean<RTYPE,NA,T> > {
30
30
public:
31
31
typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE;
32
- typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE;
33
32
typedef Rcpp::Vector<RTYPE> VECTOR;
34
33
35
34
Mean (const VEC_TYPE& object_) : object(object_) {}
36
35
37
- STORAGE get () const {
36
+ double get () const {
38
37
VECTOR input = object;
39
38
int n = input.size (); // double pass (as in summary.c)
40
39
long double s = std::accumulate (input.begin (), input.end (), 0 .0L );
@@ -89,6 +88,28 @@ class Mean<CPLXSXP,NA,T> : public Lazy<Rcomplex, Mean<CPLXSXP,NA,T> > {
89
88
const VEC_TYPE& object ;
90
89
};
91
90
91
+ template <bool NA, typename T>
92
+ class Mean <LGLSXP,NA,T> : public Lazy<double , Mean<LGLSXP,NA,T> > {
93
+ public:
94
+ typedef typename Rcpp::VectorBase<LGLSXP,NA,T> VEC_TYPE;
95
+
96
+ Mean (const VEC_TYPE& object_) : object(object_) {}
97
+
98
+ double get () const {
99
+ LogicalVector input = object;
100
+ int n = input.size (); // double pass (as in summary.c)
101
+ long double s = 0.0 ;
102
+ for (int i=0 ; i<n; i++) {
103
+ if (input[i] == NA) return NA;
104
+ s += (input[i] == TRUE );
105
+ }
106
+ s /= n; // no overflow correction needed for logical vectors
107
+ return (double )s;
108
+ }
109
+ private:
110
+ const VEC_TYPE& object ;
111
+ };
112
+
92
113
} // sugar
93
114
94
115
template <bool NA, typename T>
@@ -106,6 +127,11 @@ inline sugar::Mean<CPLXSXP,NA,T> mean(const VectorBase<CPLXSXP,NA,T>& t) {
106
127
return sugar::Mean<CPLXSXP,NA,T>(t);
107
128
}
108
129
130
+ template <bool NA, typename T>
131
+ inline sugar::Mean<LGLSXP,NA,T> mean (const VectorBase<LGLSXP,NA,T>& t) {
132
+ return sugar::Mean<LGLSXP,NA,T>(t);
133
+ }
134
+
109
135
} // Rcpp
110
136
#endif
111
137
0 commit comments