在 pgfplotstable 末尾添加 sum/mean/std 的行

在 pgfplotstable 末尾添加 sum/mean/std 的行

如何在 pgfplotstable 中的表格底部添加一行或多行,作为该特定列中上述数据的总和/平均值/方差或其他函数?

如何针对表中的某些列完成此操作?

答案1

这个答案很大程度上受到了启发使用 pgfplotstable 创建列联表(我甚至偷了示例表)。第二部分(只处理一些列)借鉴了如何在 pgfplots 中标记/标记文件中的第 n 个数据点?

基本思想是:使用从其他列计算的条目向表中添加列相对容易,但无法添加行。因此,我们转置,然后添加列,然后再次转置。由于选择列的子集可能不是每个人都需要的,因此下面的解决方案包含一个宏,\addstatistics它为所有列添加总和、平均值和标准差,以及一个宏\addsomestatistics,它接受列列表作为第三个参数(tikz 列表格式中介于 0 和数据列数减一之间的数字foreach)。两种情况下的前两个参数都是表和标签列的名称。请参阅代码以了解如何使用它的示例。

\documentclass{report}
\usepackage{booktabs}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.11}

\newcommand{\addstatistics}[2]{ %
% #1=table name
% #2=first column name
% Transpose the table, so we can work on rows instead of columns
\pgfplotstabletranspose[colnames from={#2},input colnames to={#2}]{\intermediatetable}{#1}
%
% Sums for each column
\pgfplotstablecreatecol[
    create col/assign/.code={%
        \def\colsum{0}
        \pgfmathtruncatemacro\maxcolindex{\pgfplotstablecols-1}
        \pgfplotsforeachungrouped \col in {1,...,\maxcolindex}{
            \pgfmathsetmacro\colsum{\colsum+\thisrowno{\col}}
        }
        \pgfkeyslet{/pgfplots/table/create col/next content}\colsum
    }
]{Sum}\intermediatetable
%
\pgfplotstablecreatecol[
    create col/expr={\thisrow{Sum}/(\pgfplotstablecols-2)}
]{Mean}\intermediatetable

% Standard deviation for each column
\pgfplotstablecreatecol[
    create col/assign/.code={%
        \def\colsumdevsquares{0}
        \pgfmathtruncatemacro\maxcolindex{\pgfplotstablecols-3}  % ignore the sum and the mean column!
        \pgfplotsforeachungrouped \col in {1,...,\maxcolindex}{
            \pgfmathsetmacro\colsumdevsquares{\colsumdevsquares+(\thisrowno{\col}-\thisrow{Mean})^2}
        }
        \pgfmathsetmacro\colstd{sqrt(\colsumdevsquares/(\maxcolindex-1))}%
        \pgfkeyslet{/pgfplots/table/create col/next content}\colstd
    }
]{Standard Deviation}\intermediatetable
%
% Transpose back to the original form
\pgfplotstabletranspose[colnames from=#2, input colnames to=#2]{\statstable}{\intermediatetable}
}
%

\newcommand{\addsomestatistics}[3]{ %
% #1=table name
% #2=first column name
% #3=list of columns to decorate
% Transpose the table, so we can work on rows instead of columns
\pgfplotstabletranspose[colnames from={#2},input colnames to={#2}]{\intermediatetable}{#1}
%
% Sums for each column
\pgfplotstablecreatecol[
    create col/assign/.code={%
      \def\entry{}
      \foreach \i in {#3} {
        \ifnum\pgfplotstablerow=\i
          \def\colsum{0}
          \pgfmathtruncatemacro\maxcolindex{\pgfplotstablecols-1}
          \pgfplotsforeachungrouped \col in {1,...,\maxcolindex}{
              \pgfmathsetmacro\colsum{\colsum+\thisrowno{\col}}
          }
          \xdef\entry{\colsum}
        \fi
      }
      \pgfkeyslet{/pgfplots/table/create col/next content}\entry
    }
]{Sum}\intermediatetable
%
\pgfplotstablecreatecol[
    create col/assign/.code={%
      \def\entry{}
      \foreach \i in {#3} {
        \ifnum\pgfplotstablerow=\i
          \pgfmathsetmacro\colmean{\thisrow{Sum}/(\pgfplotstablecols-2)}
          \xdef\entry{\colmean}
        \fi
      }
      \pgfkeyslet{/pgfplots/table/create col/next content}\entry
    }
]{Mean}\intermediatetable

% Standard deviation for each column
\pgfplotstablecreatecol[
    create col/assign/.code={%
      \def\entry{}
      \foreach \i in {#3} {
        \ifnum\pgfplotstablerow=\i
          \def\colsumdevsquares{0}
          \pgfmathtruncatemacro\maxcolindex{\pgfplotstablecols-3}  % ignore the sum and the mean column!
          \pgfplotsforeachungrouped \col in {1,...,\maxcolindex}{
            \pgfmathsetmacro\colsumdevsquares{\colsumdevsquares+(\thisrowno{\col}-\thisrow{Mean})^2}
          }
          \pgfmathsetmacro\colstd{sqrt(\colsumdevsquares/(\maxcolindex-1))}%
          \xdef\entry{\colstd}
        \fi
      }
      \pgfkeyslet{/pgfplots/table/create col/next content}\entry
    }
]{Standard Deviation}\intermediatetable
%
% Transpose back to the original form
\pgfplotstabletranspose[colnames from=#2, input colnames to=#2]{\statstable}{\intermediatetable}
}
%

\begin{document}

\pgfplotstableread{
    Gender Right-handed Left-handed Ambidextrous
    Male             43           9           10
    Female           44           4            5
    Other            20           3            3
}\chisquaredata

\addstatistics{\chisquaredata}{Gender}

\pgfplotstabletypeset[
  every head row/.style={%
    before row={\toprule 
        & \multicolumn{3}{c}{Handedness}\\
        \cmidrule{2-4}},
    after row=\midrule},
  every last row/.style={after row=\bottomrule},
  columns/Gender/.style={string type},
  columns={Gender, Right-handed, Left-handed, Ambidextrous},
]\statstable

\addsomestatistics{\chisquaredata}{Gender}{0,2}

\pgfplotstabletypeset[
  every head row/.style={%
    before row={\toprule 
        & \multicolumn{3}{c}{Handedness}\\
        \cmidrule{2-4}},
    after row=\midrule},
  every last row/.style={after row=\bottomrule},
  columns/Gender/.style={string type},
  columns={Gender, Right-handed, Left-handed, Ambidextrous},
]\statstable

\end{document}

两张桌子

相关内容