How to count the occurence of M and F in a particular column , No. of male and female deaths?
A variant using datatool
and some counters.
\documentclass[11pt]{report}
\usepackage{filecontents}
% this just saves the data to a text file
\begin{filecontents*}{deathdata.txt}
Sl.,Unique ID,Name,Gender,Death
1,DBC0001,Mr. John,M,Y
2,DBC0002,Miss Elizabeth,F,Y
3,DBC0003,Mr. Thomas,M,N
4,DBC0006,Miss Maya,F,N
5,DBC0005,Mr. Gilbert,M,N
6,DBC0004,Mr. Dinesh,M,N
7,DBC0004,Miss Shanti,F,Y
8,DBC0004,Mr. Rajesh,M,N
9,DBC0004,Mr. Pinku,M,N
10,DBC0004,Mr. Virendra,M,N
\end{filecontents*}
\usepackage[english]{babel}
\usepackage{graphicx} %for including eps graphics
\usepackage{booktabs}
\usepackage{xstring} % for string comparison
\usepackage{datatool}
\renewcommand{\dtldisplaystarttab}{\toprule}
\renewcommand{\dtldisplayafterhead}{\midrule}
\renewcommand{\dtldisplayendtab}{\\\bottomrule}
% read in text file to database
\DTLloaddb{death}{deathdata.txt}
% set up counters
\newcounter{Female}
\newcounter{Male}
\newcounter{FemaleD} % D for dead
\newcounter{MaleD}
\newcounter{Total}
\newcounter{TotalD}
\begin{document}
\begin{center}
\begin{tabular}{cclcc}
\toprule
Sl. & Unique ID & Name & Gender& Death
\DTLforeach*{death}{\sl=Sl.,\id=Unique ID,\name=Name,\g=Gender,\d=Death}{% build table
% add \midrule before first data row
\DTLiffirstrow{\\\midrule}{\\}%
%
\IfStrEq{\g}{F}{% check if female
\stepcounter{Female}% add 1 to counter
\IfStrEq{\d}{Y}{\stepcounter{FemaleD}}{}% if also dead, add 1 to that counter
}{%
\stepcounter{Male}% similar for male
\IfStrEq{\d}{Y}{\stepcounter{MaleD}}{}%
}%
\sl & \id & \name & \g & \d % print table row
}
\\
\bottomrule
\end{tabular}
\end{center}
% set total values
\setcounter{Total}{\numexpr\value{Female}+\value{Male}\relax}
\setcounter{TotalD}{\numexpr\value{FemaleD}+\value{MaleD}\relax}
% print results
\begin{tabular}{ll}
Occurence of M: & \theMale \\
Occurence of F: & \theFemale \\
\midrule
Total: & \theTotal
\end{tabular}
\bigskip
\begin{tabular}{ll}
No. of Male Death: & \theMaleD \\
No. of Female Death: & \theFemaleD\\
\midrule
Total: & \theTotalD
\end{tabular}
\end{document}
Here, if you place your table data in a \def
, you can use the created macro \analyze
to look for patterns. This macro employs the listofitems
package to parse the data in your table, and count the results. Because the patterns are precisely specified, it relies on you the user applying space padding around the gender and death columns (that is & M & Y
will not be interpreted the same as &M&Y
).
That being said, the results of \analyze
are stored in \males
, \females
, \maledeaths
, and \femaledeaths
, which can be later recalled.
\documentclass[11pt]{report}
\usepackage[english]{babel}
\usepackage{graphicx} %for including eps graphics
\usepackage{booktabs}
\usepackage{spreadtab}
\usepackage{listofitems}
\newcommand\analyze[1]{%
\setsepchar{& M &}%
\readlist*\males{#1}%
\xdef\males{\the\numexpr\listlen\males[]-1\relax}%
\setsepchar{& F &}%
\readlist*\females{#1}%
\xdef\females{\the\numexpr\listlen\females[]-1\relax}%
\setsepchar{& M & Y}%
\readlist*\maledeaths{#1}%
\xdef\maledeaths{\the\numexpr\listlen\maledeaths[]-1\relax}%
\setsepchar{& F & Y}%
\readlist*\femaledeaths{#1}%
\xdef\femaledeaths{\the\numexpr\listlen\femaledeaths[]-1\relax}
}
\begin{document}
\def\mydata{
1 & DBC0001 & Mr. John & M & Y \\
2 & DBC0002 & Miss Elizabeth & F & Y \\
3 & DBC0003 & Mr. Thomas & M & N \\
4 & DBC0006 & Miss Maya & F & N \\
5 & DBC0005 & Mr. Gilbert & M & N \\
6 & DBC0004 & Mr. Dinesh & M & N \\
7 & DBC0007 & Miss Shanti & F & Y \\
8 & DBC0009 & Mr. Rajesh & M & N \\
9 & DBC0008 & Mr. Pinku & M & N \\
10 & DBC0010 & Mr. Virendra & M & N \\
}
\analyze\mydata
\begin{tabular}{c|c|l|c|c|}
\toprule
Sl. & Unique ID & Name & Gender& Death \\
\midrule
\mydata
\end{tabular}
\vspace{4mm}
\hrule
\vspace{4mm}
\noindent Occurence of M: \males\\
Occurence of F : \females\\
\rule[-0.5ex]{3cm}{1.0pt}\\
\noindent Total : \the\numexpr\males+\females\relax
\vspace{2mm}
\hrule
\vspace{4mm}
\noindent No. of Male Death ~~: \maledeaths \\
No. of Female Death: \femaledeaths\\
\rule[-0.5ex]{3cm}{1.0pt}\\
\noindent Total : \the\numexpr\maledeaths+\femaledeaths\relax
\end{document}