3

この表をで印刷しようとしてxtable()います。以下に再現します。

                   X            B.1            B.2            B.3
1        (Intercept) -1.669 (0.093) -1.701 (0.094) -1.774 (0.121)
2          SEXFemale  -0.46 (0.023)  -0.386 (0.04)  -0.274 (0.17)
3    SEXFemale:BLACK                                0.132 (0.163)
4    SEXFemale:ASIAN                               -0.063 (0.089)
5 SEXFemale:HISPANIC                               -0.128 (0.074)

しかし、最初の列が印刷される方法をオーバーライドしたいと思います。これを行うためにラテックスコマンドxtable()または他の機能を含めることは可能ですか? 具体的には、最初の列を次のようにしたいと思います。

    X
Male: Female
Female X Black
       X Asian
       X Hispanic

\phantom{}つまり、列にスイッチを書き込んで、間隔が正しくなるようにしたいと思います。

4

2 に答える 2

2

を呼び出す前に、正規表現を使用し\phantom{}て をテーブルに挿入できます。xtable()

データを再作成します。

x <- structure(list(X = structure(c(1L, 2L, 4L, 3L, 5L), .Label = c("(Intercept)", 
                                                                    "SEXFemale", "SEXFemale:ASIAN", "SEXFemale:BLACK", "SEXFemale:HISPANIC"
), class = "factor"), B.1 = structure(c(3L, 2L, 1L, 1L, 1L), .Label = c("", 
                                                                        "-0.46 (0.023)", "-1.669 (0.093)"), class = "factor"), B.2 = structure(c(3L, 
                                                                                                                                                 2L, 1L, 1L, 1L), .Label = c("", "-0.386 (0.04)", "-1.701 (0.094)"
                                                                                                                                                 ), class = "factor"), B.3 = structure(c(4L, 3L, 5L, 1L, 2L), .Label = c("-0.063 (0.089)", 
                                                                                                                                                                                                                         "-0.128 (0.074)", "-0.274 (0.17)", "-1.774 (0.121)", "0.132 (0.163)"
                                                                                                                                                 ), class = "factor")), .Names = c("X", "B.1", "B.2", "B.3"), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                  -5L))

正規表現:

x$X <- gsub("SEXFemale:", "\\\\phantom{Female} X ", x$X)
x$X <- gsub("SEXFemale", "Female", x$X)


library(xtable)
xx <- print(xtable(x), print.results = FALSE, include.rownames = FALSE,
        sanitize.text.function=function(x)x)
cat(xx)

結果のテキスト:

% latex table generated in R 2.15.0 by xtable 1.7-0 package
% Thu Jul 26 17:10:05 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{llll}
  \hline
X & B.1 & B.2 & B.3 \\ 
  \hline
(Intercept) & -1.669 (0.093) & -1.701 (0.094) & -1.774 (0.121) \\ 
  Female & -0.46 (0.023) & -0.386 (0.04) & -0.274 (0.17) \\ 
  \phantom{Female} X BLACK &  &  & 0.132 (0.163) \\ 
  \phantom{Female} X ASIAN &  &  & -0.063 (0.089) \\ 
  \phantom{Female} X HISPANIC &  &  & -0.128 (0.074) \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

そして最終的な出力:

ここに画像の説明を入力

于 2012-07-26T16:13:09.730 に答える