2

I am new on this forum and have a question about transposing data with the pgfplotstable package. I have found a post explaining how to transpose a table and have tried to make modifications using the documentation but I am not managing to find the right commands.

I have an input CSV file which contains a series of values (one line only):

2016-06-13 05:00,6.2,5305,849,29.0,160,1,160

I want to use LaTeX

  1. to read the file,
  2. generate a vertical table with the values, and
  3. assign an arbitrary name for each line of the output table.

The code I have for this:

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

\begin{document}

\pgfplotstableread[
    col sep=comma,
]{test.csv}\normal

\pgfplotstabletranspose[
%    header=false
    input colnames to={colA,colB}
]\transpose\normal

%\begin{center}
\pgfplotstabletypeset[
string type,
every head row/.style={before row=\toprule, after row=\midrule},
every last row/.style={after row=\bottomrule},
every col no 0/.style={
    column type={l},
    column name={Averages for the week},
    %postproc cell content/.append style={/pgfplots/table/@cell content/.add={Subj }{}},
    },
]\transpose

\end{document}

With this code, I get a table with only one column (and some text appended at the end of the last row "index7"), whereas I would like to have something like this (where I define in the TEX file names for each rows:

wanted output

Is it possible to do this?

Thank you very much in advance for your replies.

  • Welcome to TeX.SX!! Nice question. Rather than posting code fragments it is better to post a full minimal working example. A MWE should start with a \documentclass command, have a minimal preamble and then \begin{document}...\end{document}. The code should compile and be as small as possible to demonstrate your problem. This makes it much easier for people to help you --- and much more likely that they will! – Andrew Aug 8 '16 at 10:40
1

Maybe something like this:

\documentclass{article}
\usepackage{readarray,filecontents,stackengine,booktabs}
\begin{filecontents*}{mydata.dat}
2016-06-13 05:00,6.2,5305,849,29.0,160,1,160
\end{filecontents*}
\setstackEOL{,}
\renewcommand\stackalignment{r}
\def\sepdata#1,#2\relax{\def\Week{#1}\def\datalist{#2}}
\newcommand\readparsedef[1]{\readdef{#1}{\data}\expandafter\sepdata\data\relax}
\begin{document}
\readparsedef{mydata.dat}
\begin{tabular}{lr}
\toprule
Averages for the week starting & \Week\\
\midrule
\Longstack[l]{Row Name1,
Row Name2,
Row Name3,
Row Name4,
Row Name5,
Row Name6,
Row Name7} & 
\expandafter\Longstack\expandafter{\datalist}\\
\bottomrule
\end{tabular}
\end{document}

enter image description here

0

It is possible using \pgfplotstable, just very very difficult.

\documentclass{article}
\usepackage{pgfplotstable,filecontents,booktabs}
\begin{filecontents*}{test.dat}
2016-06-13 05:00,6.2,5305,849,29.0,160,1,160
\end{filecontents*}

\begin{document}
\pgfplotstableread[col sep=comma,header=false,string type]{
Averages for week starting,RowName1,RowName2,RowName3,RowName4,RowName5,RowName6,RowName7
}\names% create column names
\fbox{\pgfplotstabletypeset[string type]\names}% debug

\pgfplotstableread[
    header=false,
    col sep=comma,
    string type
]{test.dat}\normal
\fbox{\pgfplotstabletypeset[string type]\normal}% debug

\pgfplotstablegetelem{0}{0}\of\normal
\let\mydate=\pgfplotsretval% will become column name later
\mydate% debug

\pgfplotstablevertcat\names\normal
\fbox{\pgfplotstabletypeset[string type,]\names}% debug

\pgfplotstabletranspose[header=false,colnames from=0]\transpose\names
\fbox{\pgfplotstabletypeset[string type]\transpose}% debug

\pgfplotstabletypeset[string type,columns={Averages for week starting,\mydate},
  columns/Averages for week starting/.style={column type=l},
  columns/\mydate/.style={column type=r},
  ]\transpose

\end{document}

demo

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.