Skip to content

Commit

Permalink
table: summary: omit NaNs from mean(), include NaN counts
Browse files Browse the repository at this point in the history
  • Loading branch information
apjanke committed Feb 7, 2024
1 parent 876cc52 commit ee4cd7d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
9 changes: 5 additions & 4 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ Tablicious Changelog
Unreleased (in progress)
-----------------------

* Add `end` override methods to `string`, `datetime`, etc. so `x(end)` indexing works correctly.
* Add `end` override methods to string, datetime, etc. so `x(end)` indexing works correctly.
* table: summary: omit NaNs from mean(), include NaN counts

0.4.1 (2024-02-07)
------------------

### Bug Fixes

* `string`:
* `string`: Fix missing trailing newline in string display in `string.disp`.
* `string`: Add `+` operator overload that does string concatenation.
* string:
* string: Fix missing trailing newline in string display in `string.disp`.
* string: Add `+` operator overload that does string concatenation.

Version 0.4.0 (2024-02-07)
--------------------------
Expand Down
48 changes: 48 additions & 0 deletions inst/+tblish/+internal/nanmean.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## Copyright (C) 2024 Andrew Janke <floss@apjanke.net>
##
## This file is part of Tablicious.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, see
## <https://www.gnu.org/licenses/>.

function out = nanmean (x, dim)
# mean, omitting NaNs
#
# This function exists so that we can do this on Octaves prior to 7.x, which did
# not support the 'omitnan'option.
#
# This is a minimal implementation that supports only the mean calculations that table
# needs.
if (ndims (x) > 2)
error ('tblish.internal.nanmean: only 2-D inputs are supported');
endif

if (isvector (x) && nargin == 1)
x2 = x (~isnan(x));
out = mean (x2);
else
if (nargin < 2 || isempty (dim)); dim = 1; endif
if (dim == 1)
out = NaN (1, size (x, 2));
for i = 1:size (x, 2)
out(i) = tblish.internal.nanmean (x(:,i));
endfor
elseif (dim == 2)
outInv = tblish.internal.nanmean (x', 1);
out = outInv';
else
error ('tblish.internal.nanmean: invalid dim argument: %d', dim)
endif
endif
endfunction
13 changes: 9 additions & 4 deletions inst/@table/table.m
Original file line number Diff line number Diff line change
Expand Up @@ -3267,10 +3267,12 @@ function summary_impl (this, fmt)
endfunction

function out = summary_for_var_numeric (x)
x_min = min (x);
x_mean = mean (x);
x_max = max (x);
x_prcts = prctile (x, [25 50 75]);
xvec = x(:);
x_min = min (xvec);
x_mean = tblish.internal.nanmean (xvec);
x_max = max (xvec);
x_prcts = prctile (xvec, [25 50 75]);
n_nans = sum (isnan (xvec));
out = {
'Min.' num2str(x_min)
'1st Qu.' num2str(x_prcts(1))
Expand All @@ -3279,6 +3281,9 @@ function summary_impl (this, fmt)
'3rd Qu.' num2str(x_prcts(3))
'Max.' num2str(x_max)
};
if (n_nans > 0)
out = [out; {'N. NaN', num2str(n_nans)}];
endif
endfunction

function out = summary_for_var_categorical (x)
Expand Down

0 comments on commit ee4cd7d

Please sign in to comment.