2

I want to sum the numbers of the last 3 columns in mytable (see below). If all the dates in column A are different, the MWE works fine giving the proper sums (see MWE). But I wish to list Date-1 in only the first row of column A in and leave the remaining rows of column A blank. In this latter situation, the program fails giving an error message: "...an unbalanced number of columns...". Since all the data refers to Date-1 (not Date-2, Date-3, etc), I'd really appreciate a solution. Thanks.

\documentclass{article}
\usepackage{booktabs}
\usepackage{pgfplotstable}


\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
%
% Transpose back to the original form
\pgfplotstabletranspose[colnames from=#2, input colnames to=#2]{\statstable}{\intermediatetable}
}
%


\begin{document}


\pgfplotstableread{
    A B C D E F
   Date-1   Ken Tod 202  12  0
   Date-2      Wood    Haz 317 1 0
   Date-3     Front    -   102 10  0
    Date-4     Front Haz 145 11  0
 }\mytable

\addsomestatistics{\mytable}{A}{2,3,4}

\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/A/.style={string type},
  columns/B/.style={string type},
   columns/C/.style={string type},
  columns={A,B, C, D, E, F},
]\statstable

\end{document}
%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.