0

I have a regression table in latex that is the result of output from applying the stargazer package on my regression results in R. I am wondering how to transpose the table such that the columns switch place with rows. In other words, I want the Lags to be where the variables are and vice versa.

Moreover, I am wondering how to reduce the space between the first and second columns as well as how to add a vertical line between columns. I've tried adding | in between, but sometimes this doesn't cover well and leaves in some space in between the top and bottom hline.

 \usepackage{dcolumn}
 \begin{table}[!htbp] \centering 
  \caption{Regression results} 
  \label{} 
    \setlength{\tabcolsep}{0pt}
\begin{tabular}{lD{.}{.}{-2} D{.}{.}{-2} D{.}{.}{-2} D{.}{.}{-2} D{.}{.}{-2} D{.}{.}{-2}} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \multicolumn{6}{c}{Characteristics} \\ 
\cline{2-7} 
 \\ 
 Lags: & \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{5} & \multicolumn{1}{c}{6} \\ 
\hline \\[-1.8ex] 
 Variable 1 & -1.14^{***} & 0.04 & -1.73^{***} & -0.24 & -0.59 & -1.64^{***} \\ 
  Variable 2 & 0.16 & -0.25 & 0.12 & -0.12 & 0.14{**} & 0.04  \\ 
  Variable 3 & 0.46^{*} & -0.61 & 0.17 & -0.98^{***} & -0.32 & -0.09\\ 
  Variable 4 & -0.02 & -0.005 & -0.03^{***} & -0.32 & -0.11 & 0.015 \\ 
 \hline \\[-1.8ex] 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{6}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\end{tabular} 
\end{table}

Code result

Andycode
  • 1
  • 1
  • Welcome to TeX.SX! Are you aware that your code is not compilable? You can not have a \multicolumn{18} in a table that has only 7 columns. To get rid of the error message and simultaneously solve issue 2, you can use \multicolumn{7}{l}{\textit{Note:} $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01}. – leandriis May 19 '20 at 17:31
  • Hi @leandriis, thx for your comment. This compiles in my overleaf because this example is just something I put together from an already existing table with 18 columns so I missed that adjustment! – Andycode May 19 '20 at 17:34
  • \textit{Note:} & \multicolumn{7} will still give you an error message since this adds up to 8 columns. Either place the "Note" inside the multicolumn as well or use \multicolumn{6} – leandriis May 19 '20 at 17:36
  • 1
    The vertical line nor working well with your table is due to the use of \\[-1.8ex]\hline \hline \\[-1.8ex] for the horizontal lines above the table. Insetad of that, I would use \toprule from the booktabs package. (However, the booktabs lines are incompatible with vertical lines, thus, I would not use a vertical line at all.) – leandriis May 19 '20 at 17:38
  • Regarding transposing the table: Is there a special reason why you want to do that? Since the table is already narrower than the textwidth and thus fits into the available space perfectly fine, I don't think you can save space by transposing the table. – leandriis May 19 '20 at 17:40
  • @leandriis Yes, my original table has 18 columns and only 6-7 variables. Therefore it seems better to have the table transposed. I just shortened the code and table for the purpose of asking the question. I already have horizontal tables with 18 columns where i flipped the PDF page, but I want vertical for this table as it shows something different than the others and I would like this to be clearer in my paper by having the table differently. – Andycode May 19 '20 at 17:44
  • Thanks for your clarification. I assume you want to automatically transpose the table. Probably the answer to https://tex.stackexchange.com/q/75793/134144 could help with that. – leandriis May 19 '20 at 17:47
  • I've already read those suggested solutions, but have a problem applying it to my code. E.g the pgfplotstable suggestion didn't do anything for me, so I must have done something wrong. (I am just getting started with Latex, have no previous experience). – Andycode May 19 '20 at 17:53
  • 1
    To be honest, I would try to save the contents of the table as a csv file and use excel to transpose it. You will nevertheless have to do some adjustments to the code to get a decent result. – leandriis May 19 '20 at 18:20
  • 1
    isn't it easy to transpose the data in R ? – David Carlisle May 19 '20 at 21:38
  • No, for some reason the flip argument doesn't work with stargazer in R. Might be other ways around it, but not that I'm familiar with. – Andycode May 20 '20 at 08:45
  • @Andycode If flip argument of stargazer doesn't work, before transpose your x matrix/data.frame of results with y <- t(x) and then export y to a latex table with stargazer, or xtable, or kable. If you add to the question a R code making a matrix/dataframe and the code that you use to produce the latex table, we could help from this point, ideally without having to touch up the R output. – Fran May 21 '20 at 04:28

0 Answers0