# Build a statistic with pgfplotstable

I am trying to build some sort of a statistic over a table like in the following Code:

\documentclass{standalone}

\usepackage{pgfplotstable}
\usepackage{booktabs, colortbl}
\usetikzlibrary{patterns}
\pgfplotstableset{col sep=semicolon, use comma, fixed, set thousands separator={},
every even row/.style={before row={\rowcolor[gray]{0.9}}},
every head row/.style={before row=\toprule, after row=\midrule},
every last row/.style={after row=\bottomrule}}

\newcommand{\sumoccurence}[3]{
% #1=table
% #2=column
% #3=value
\def\colsum{0}
\pgfplotstableforeachcolumnelement{#2}\of#1\as\cell{
\ifdim\cell pt=#3 pt
\pgfmathsetmacro\colsum{\colsum+1}
\fi
}
\colsum
}

% #1=table name to count
% #2=column name to count
% #4=column mith marks
% #5=name of new column
% Sums for each column
%Should read from table #1 the column #2 and count for each mark in table #3 in column #4 and add a new column #5
\pgfplotstablecreatecol[
create col/assign/.code={%
\def\entry{}
%\xdef\entry{
%\xdef\entry{\sumoccurence{#1}{#2}{\thisrowno{#2}}
%\xdef\entry{\sumoccurence{#1}{#2}{2.0}}
\xdef\entry{\thisrow{#4}}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}
]{#5}#3
}
%

\begin{document}
ID; Mark
1010121; 1.0
1010122; 1.0
1010123; 5.0
1010124; 5.0
1010125; 3.0
1010126; 4.0
1010127; 2.7
}\Marks

Marks; Count
1.0; 2
1.3; 0
1.7; 0
2.0; 0
2.3; 0
2.7; 1
3.0; 1
3.3; 0
3.7; 0
4.0; 1
4.7; 0
5.0; 2
}\Statistic

%Original Table
\pgfplotstabletypeset[columns={ID, Mark},
columns/Mark/.style={numeric type, fixed zerofill, precision=1}]\Marks

%   \hspace{1cm}

%Statistic how it should be
\pgfplotstabletypeset[
columns={Marks, Count},
columns/Marks/.style={numeric type, fixed zerofill, precision=1}]\Statistic

%Creating a new Table for the statistic
\pgfplotstablenew[create on use/Marks/.style={create col/set list={1.0,1.3,1.7,2.0,2.3,2.7,3.0,3.3,3.7,4.0,4.7,5.0}}, columns={Marks},]{12}\loadedtable

%Adding an automatically calculated column for count of each mark

%Current result with just a copy of the first column
\pgfplotstabletypeset[
columns={Marks, Count},
\end{document}


I was able to create a command that calculates all occurences (\sumoccurences) of a given value. But when I try to use this command in a new command like \addstatisticcol it doesn't work. I get lots of errors like

and like

Argument of \pgfutil@in@@ has an extra }. ...Noten}{Note}{\loadedtable}{Noten}{Anzahl}

Here is the current output:

Hopefully someone can give me a hint.

• I haven't tested the above code so far, but up to now your final goal is unclear to me. Could you edit your question and maybe state an example what an result you expect? – Stefan Pinnow Feb 15 '18 at 13:05
• @Stefan Done. :-) – baghira Feb 16 '18 at 7:51

It was just a small error. I needed to put the \sumoccurences out of the \xdef and use the macro there instead.

\newcommand{\sumoccurence}[3]{
% #1=table
% #2=column
% #3=value
\def\colsum{0}
\pgfplotstableforeachcolumnelement{#2}\of#1\as\cell{
\ifdim\cell pt=#3 pt
\pgfmathsetmacro\colsum{\colsum+1}
\fi
}
%\colsum %removed this
}

% #1=table name to count
% #2=column name to count
% #4=column mith marks
% #5=name of new column
% Sums for each column
%Should read from table #1 the column #2 and count for each mark in table #3 in column #4 and add a new column #5
\pgfplotstablecreatecol[
create col/assign/.code={%
\def\entry{}
\sumoccurence{#1}{#2}{\thisrow{#4}} %call it separately
\xdef\entry{\colsum} %use the created macro here
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}
]{#5}#3
}
%