lmccay4 commented on a change in pull request #163: KNOX-2052 - KnoxShellTable
mean, median, and mode methods
URL: https://github.com/apache/knox/pull/163#discussion_r332265264
##########
File path:
gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
##########
@@ -76,6 +80,98 @@ public KnoxShellTable value(Comparable<? extends Object>
value) {
return col;
}
+ public double[] toDoubleArray(String colName) throws
IllegalArgumentException {
+ List<Comparable<? extends Object>> col = values(colName);
+ double[] colArray = new double[col.size()];
+ int conversionMethod = 0;
+ for (int i = 0; i < col.size(); i++) {
+ if (i == 0) {
+ if (col.get(i) instanceof Integer) {
+ conversionMethod = INTEGER_CONVERSION_METHOD;
+ }
+ else if (col.get(i) instanceof Double) {
+ conversionMethod = DOUBLE_CONVERSION_METHOD;
+ }
+ else if (col.get(i) instanceof Float) {
+ conversionMethod = FLOAT_CONVERSION_METHOD;
+ }
+ else {
+ throw new IllegalArgumentException();
+ }
+ }
+ if (conversionMethod == INTEGER_CONVERSION_METHOD) {
+ colArray[i] = (double) ((Integer) col.get(i)).intValue();
+ }
+ if (conversionMethod == DOUBLE_CONVERSION_METHOD) {
+ colArray[i] = (double) ((Double) col.get(i));
+ }
+ if (conversionMethod == FLOAT_CONVERSION_METHOD) {
+ colArray[i] = (double) ((Float) col.get(i)).floatValue();
+ }
+ }
+ return colArray;
+ }
+
+ public double mean(String colName) {
+ double[] colArray = toDoubleArray(colName);
+ double arrayMean = StatUtils.mean(colArray);
+ return arrayMean;
+ }
+
+ public double mean(int colIndex) {
+ return mean(headers.get(colIndex));
+ }
+
+ public double median(String colName) {
+ double[] colArray = toDoubleArray(colName);
+ double arrayMedian = StatUtils.percentile(colArray, 50);
+ return arrayMedian;
+ }
+
+ public double median(int colIndex) {
+ return median(headers.get(colIndex));
+ }
+
+ public double mode(String colName) {
+ double[] colArray = toDoubleArray(colName);
+ double[] arrayMode = StatUtils.mode(colArray);
+ return (double) StatUtils.mode(arrayMode)[0];
+ }
+
+ public double mode(int colIndex) {
+ return mode(headers.get(colIndex));
+ }
+
+ public double sum(String colName) {
+ double[] colArray = toDoubleArray(colName);
+ double arraySum = StatUtils.sum(colArray);
+ return arraySum;
+ }
+
+ public double sum(int colIndex) {
+ return sum(headers.get(colIndex));
+ }
+
+ public double max(String colName) {
+ double[] colArray = toDoubleArray(colName);
+ double arrayMax = StatUtils.max(colArray);
+ return arrayMax;
+ }
+
+ public double max(int colIndex) {
+ return max(headers.get(colIndex));
+ }
+
+ public double min(String colName) {
+ double[] colArray = toDoubleArray(colName);
+ double arrayMin = StatUtils.min(colArray);
+ return arrayMin;
Review comment:
Thanks, will update these!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services