5

In the MWE below I am calculating percent change for some year to year data. When dividing next row value by the previous row's value the first row gets infinity due to zero division and outputs infinity symbols into the cells of Col1 and Col2. How can I empty those cells or at least replace infinity with a dash?

\documentclass{scrartcl}
\usepackage[top=1in,bottom=1in,left=1in,right=1in]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,angles,quotes,intersections,decorations.pathmorphing,positioning,matrix,fit}
\usepackage{filecontents,pgfplotstable,pgfplots,booktabs}
\pgfplotsset{compat=newest}

\begin{document}

\begin{filecontents*}{healthcare.txt}
x   Col1    Col2
2010    597.5 392.3
2011    615.1 400.9
2012    628.7 410.2
2013    634.3 417.2
2014    644.4 422.6
\end{filecontents*}

\pgfplotstableread{healthcare.txt}\datatable
\pgfplotstablecreatecol[expr={(\thisrow{Col1}-\prevrow{Col1})*100/\prevrow{Col1}}]{Col3}{\datatable}
\pgfplotstablecreatecol[expr={(\thisrow{Col2}-\prevrow{Col2})*100/\prevrow{Col2}}]{Col4}{\datatable}

\pgfplotstabletypeset[
    every head row/.style={before row={\toprule},after row=\midrule},
    columns/x/.style={column name=Year,1000 sep={}},
    columns/Col1/.style={column name=Col1,1000 sep={}},
    columns/Col2/.style={column name=Col2,1000 sep={}},
    columns/Col3/.style={column name=Col3,1000 sep={}},
    columns/Col4/.style={column name=Col4,1000 sep={}},
]{\datatable}

\end{document}

I tried skipping rows in the table, and empty cells with but this use case requires something different. I glanced over pages 40-50 of PGFPlotstable manual, but noting immediately came to mind.

enter image description here

0

1 Answer 1

6

The clear infinite key will make those cells empty.

enter image description here

\documentclass{scrartcl}
\usepackage{filecontents,pgfplotstable,booktabs}
\pgfplotsset{compat=newest}

\begin{document}

\begin{filecontents*}{healthcare.txt}
x   Col1    Col2
2010    597.5 392.3
2011    615.1 400.9
2012    628.7 410.2
2013    634.3 417.2
2014    644.4 422.6
\end{filecontents*}

\pgfplotstableread{healthcare.txt}\datatable
\pgfplotstablecreatecol[expr={(\thisrow{Col1}-\prevrow{Col1})*100/\prevrow{Col1}}]{Col3}{\datatable}
\pgfplotstablecreatecol[expr={(\thisrow{Col2}-\prevrow{Col2})*100/\prevrow{Col2}}]{Col4}{\datatable}

\pgfplotstabletypeset[
    clear infinite,
    every head row/.style={before row={\toprule},after row=\midrule},
    columns/x/.style={column name=Year,1000 sep={}},
    columns/Col1/.style={column name=Col1,1000 sep={}},
    columns/Col2/.style={column name=Col2,1000 sep={}},
    columns/Col3/.style={column name=Col3,1000 sep={}},
    columns/Col4/.style={column name=Col4,1000 sep={}},
]{\datatable}

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .