如何计算数据集中的离散变量?

如何计算数据集中的离散变量?

我正在尝试计算每个性别在表格中出现的次数。我想将这些值与其余文本一起打印出来(我不需要任何表格,但我

\documentclass{article}

\usepackage{xstring}
\usepackage{datatool}

\newcommand*{\thePassGrade}{2.0}

\begin{filecontents*}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
Weisbaeck,Werner,34567,p,5.0
Weisbaeck,Werner,34567,q,5.0
Weisbaeck,Werner,34567,q,5.0
\end{filecontents*}

\DTLloaddb{grades}{grade.csv}

\begin{document}
\newcounter{m}
\newcounter{f}
\newcounter{p}
\newcounter{q}


\DTLforeach*{grades}{\name=name,\g=gender}{
\IfStrEq{\g}{m}{\stepcounter{m}}{
\IfStrEq{\g}{f}{\stepcounter{f}}{
\IfStrEq{\g}{p}{\stepcounter{p}}{
\IfStrEq{\g}{q}{\stepcounter{q}}}}
}}



There are \them and \thef and \thep and \theq

\end{document}

我试图获取There are 2 and 1 and 1 and 2,但却收到错误。

答案1

使用\IfStrEqCase嵌套的方式更加容易\IfStrEq

enter image description here

代码:

\documentclass{article}

\usepackage{xstring}
\usepackage{datatool}

\newcommand*{\thePassGrade}{2.0}

\begin{filecontents*}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
Weisbaeck,Werner,34567,p,5.0
Weisbaeck,Werner,34567,q,5.0
Weisbaeck,Werner,34567,q,5.0
\end{filecontents*}

\DTLloaddb{grades}{grade.csv}

%% No need for this to be after \begin{document}
\newcounter{m}
\newcounter{f}
\newcounter{p}
\newcounter{q}


\begin{document}


\DTLforeach*{grades}{\name=name,\g=gender}{%
    \IfStrEqCase{\g}{%
        {m}{\stepcounter{m}}%
        {f}{\stepcounter{f}}%
        {p}{\stepcounter{p}}%
        {q}{\stepcounter{q}}%
    }%
}



There are \them\ and \thef\ and \thep\ and \theq.

\end{document}

相关内容