FREQUENCIES: Use the smallest mode if there are multiple.
[pspp] / src / language / stats / frequencies.c
index a851c2196073a4ba1d3b530f70c686aca323e19f..96d903c5b7238cf1bbd514f8c0527a24803ef7c7 100644 (file)
@@ -359,7 +359,8 @@ calc_percentiles (const struct frq_proc *frq, struct var_freqs *vf)
   if (!frq->n_percentiles)
     return;
 
-  vf->percentiles = xnmalloc (frq->n_percentiles, sizeof *vf->percentiles);
+  if (!vf->percentiles)
+    vf->percentiles = xnmalloc (frq->n_percentiles, sizeof *vf->percentiles);
 
   const struct freq_tab *ft = &vf->tab;
   const double W = ft->valid_cases;
@@ -1487,21 +1488,14 @@ calc_stats (const struct frq_proc *frq, const struct var_freqs *vf,
   int most_often = -1;
   double X_mode = SYSMIS;
 
-  /* Calculate the mode. */
+  /* Calculate the mode.  If there is more than one mode, we take the
+     smallest. */
   for (f = ft->valid; f < ft->missing; f++)
-    {
-      if (most_often < f->count)
-        {
-          most_often = f->count;
-          X_mode = f->values[0].f;
-        }
-      else if (most_often == f->count)
-        {
-          /* A duplicate mode is undefined.
-             FIXME: keep track of *all* the modes. */
-          X_mode = SYSMIS;
-        }
-    }
+    if (most_often < f->count)
+      {
+        most_often = f->count;
+        X_mode = f->values[0].f;
+      }
 
   /* Calculate moments. */
   m = moments_create (MOMENT_KURTOSIS);