データフレーム・行列を LaTeX の表にする Last modified: Mar 20, 2006
目的
R のデータフレームまたは行列を,LaTeX ソースに変換する関数。
(R の MISC には,latex 関数があるが,Macintosh 用には用意されていないようなので)
使用法
print.latex(x, format="書式", ...)
print(x, format="書式", ...) # x のクラスが "latex" のときにはこのように省略することも可
引数
x, # データフレームまたは行列。クラスとして "latex" を与えるとよいこともある
file="" # LaTeX ソースの出力ファイル。デフォルトは画面へ出力。
append=FALSE # ファイルが存在するときは上書きする。追加するなら append=TRUE
ctable=TRUE # ctable.sty を使う。通常の表を作るときには ctable=FALSE
caption="" # 表のタイトル \caption{}
cap="" # 表のタイトル(短縮版 ctable のときのみ有効)
label="" # \label{}
htbp="htbp" # ページ内の配置指示 [htbp]
align=NULL # カラム内の配置指示 "rlcrlcrlc" など
format=NULL # 表示書式。format="f5 i s" など
tnote=NULL # tnote (ctable のみ)
line.feed=NULL # \\[nn] (ctable でないとき) "-1.5mm" など
na2blank=TRUE # NA を空白に置き換える。NA のままのときには FALSE
ソース
インストールは,以下の 1 行をコピーし,R コンソールにペーストする
source("http://aoki2.si.gunma-u.ac.jp/R/src/print.latex.R", encoding="euc-jp")
# データフレーム・行列を LaTeX の表にする
print.latex <- function(
x, # matrix or data frame; "latex" class
file="", # output file (default to screen)
append=FALSE, # rewrite or append output file
ctable=TRUE, # use ctable.sty or not
caption="", # caption
cap="", # short caption (ctable only)
label="", # label
htbp="htbp", # layout ex. htpb="ht" or "p" etc.
align=NULL, # algnment of column ex. align="rlcrlcrlc"
format=NULL, # format of column ex. format="f5 2i s" == "%.5f %i %i %s"
tnote=NULL, # tnote (ctable only) ex. tnote=c("xxx...", "yyy...",...)
line.feed=NULL, # \\[nn] (not for ctable) ex. line.feed="-1.5mm" etc.
na2blank=TRUE # display blank for NA values
)
{
nc <- ncol(x) # オブジェクトの列数
nr <- nrow(x) # オブジェクトの行数
has.rowname <- !is.null(rownames(x)) # 行名がある
if (is.null(colnames(x))) { # 列名がないときは,
colnames(x) <- paste("Col", 1:nc, sep="-") # "Col-##" の形式で名前を付ける
}
if (is.null(align)) { # align の指定がないときは,
align <- paste(rep("c", nc+has.rowname), collapse="") # センター揃え "c…" にする
}
if (is.null(format)) { # format の指定がないときは,
format <- rep("%s", nc) # "%s" で書く
}
else { # format の指定があるときは,
format <- unlist(strsplit(format, "[ ,]")) # 文字ベクトルに分解
format <- unlist(sapply(format, function(str) { # Rf3 のような形式(R 繰り返し)に対応
rep <- sub("[a-z]+[0-9]*", "", str)
if (rep == "") {
rep <- "1"
}
rep <- as.integer(rep)
fmt <- sub("^[0-9]*", "", str)
return(rep(fmt, rep))
}))
if (nc+has.rowname > length(format)) stop("too few formats") # 行名がないときは列数に等しいこと,行名があれば列数プラス1
else if (nc+has.rowname < length(format)) stop("too many formats")
format <- sapply(1:(nc+has.rowname), function(i) { # 正式な format に変換する
fmt <- format[i]
ch <- substring(fmt, 1, 1)
if (ch == "f") {
format[i] <- paste("%.", substring(fmt, 2), "f", sep="")
}
else {
format[i] <- paste("%", ch, sep="")
}
})
}
cat("%\n", file=file, append=append, sep="") # ヘッダー
if (ctable) { # ctable 形式で出力する場合
cat( "\\ctable[\n",
"\tcap = {", cap, "},\n",
"\tcaption = {", caption, "},\n",
"\tlabel = ", label, ",\n",
"\tpos = htbp\n",
"]\n",
"{", align, "}\n",
file=file, append=append, sep="")
if (is.null(tnote)) {
cat("{}\n", file=file, append=append, sep="")
}
else {
cat("{\n", file=file, append=append, sep="")
sapply(1:length(tnote),
function(i)
cat("\\tnote[", letters[i], "]{", tnote[i], "}\n", file=file, append=append, sep=""))
cat("}\n", file=file, append=append, sep="")
}
cat("{ \\FL\n", file=file, append=append, sep="")
head <- sprintf("\t %s %s \\ML\n", if (has.rowname) "&" else "", paste(colnames(x), collapse=" & "))
cat(head, file=file, append=TRUE)
for (i in 1:nr) {
temp <- sapply(1:nc, function(j) sprintf(format[j+has.rowname], x[i, j]))
if (has.rowname) {
body <- sprintf("\t %s & %s", sprintf(format[1], rownames(x)[i]), paste(temp, collapse=" & "))
}
else {
body <- sprintf("\t %s", paste(temp, collapse=" & "))
}
if (na2blank) { # na2blank == TRUE なら,
body <- gsub("(^NA | NA | NA$)", " ", body) # NA を出力しない(空白を出力する)
}
if (i == nr) {
body <- sprintf("%s \\LL\n", body)
}
else {
body <- sprintf("%s \\NN\n", body)
}
cat(body, file=file, append=TRUE)
}
cat("}\n", file=file, append=TRUE, sep="")
}
else { # table 形式で出力する場合
cat( "\\begin{table}[", htbp, "]\n",
"\t\\caption{", caption, "}\n",
"\t\\label{", label, "}\n",
"\t\\centering\n",
"\t\t\\begin{tabular}{", align, "} \\hline\n",
file=file, append=append, sep="")
head <- sprintf("\t\t\t\t %s %s \\\\ \\hline\n", if (has.rowname) "&" else "", paste(colnames(x), collapse=" & "))
cat(head, file=file, append=TRUE)
for (i in 1:nr) {
temp <- sapply(1:nc, function(j) sprintf(format[j+has.rowname], x[i, j]))
if (has.rowname) {
body <- sprintf("\t\t\t %s & %s", sprintf(format[1], rownames(x)[i]), paste(temp, collapse=" & "))
}
else {
body <- sprintf("\t\t\t %s", paste(temp, collapse=" & "))
}
if (na2blank) { # na2blank == TRUE なら,
body <- gsub("(^NA | NA | NA$)", " ", body) # NA を出力しない(空白を出力する)
}
if (i == nr) {
body <- sprintf("%s \\\\ \\hline\n", body)
}
else if (is.null(line.feed)) {
body <- sprintf("%s \\\\\n", body)
}
else {
body <- sprintf("%s \\\\[%s]\n", body, line.feed)
}
cat(body, file=file, append=TRUE)
}
cat("\t\t\\end{tabular}\n", "\\end{table}\n", file=file, append=TRUE, sep="")
}
cat("%\n", file=file, append=TRUE, sep="") # フッター
}
使用例
> y <- matrix(rnorm(15),3)
> print.latex(y,format="f1,f2,f3,f4,f5")
> print(y,format="f1,f2,f3,f4,f5")
%
\ctable[
cap = {},
caption = {},
label = ,
pos = htbp
]
{ccccc}
{}
{ \FL
Col-1 & Col-2 & Col-3 & Col-4 & Col-5 \ML
0.8 & 0.42 & -0.494 & -0.5716 & 0.06804 \NN
-0.7 & 0.55 & -0.977 & -2.6457 & -0.49711 \NN
1.2 & -0.15 & -0.209 & -1.1252 & -0.26397 \LL
}
%
> print.latex(y,format="f1,f2,f3,f4,f5",ctable=F)
%
\begin{table}[htbp]
\caption{}
\label{}
\begin{center}
\begin{tabular}{ccccc} \hline
Col-1 & Col-2 & Col-3 & Col-4 & Col-5 \\ \hline
0.8 & 0.42 & -0.494 & -0.5716 & 0.06804 \\
-0.7 & 0.55 & -0.977 & -2.6457 & -0.49711 \\
1.2 & -0.15 & -0.209 & -1.1252 & -0.26397 \\ \hline
\end{tabular}
\end{center}
\end{table}
%
> class(y) <- "latex"
# latex クラスにすると,print 関数は print.latex を呼ぶ。
> print(y,format="f1,f2,f3,f4,f5",ctable=F)
%
\begin{table}[htbp]
\caption{}
\label{}
\begin{center}
\begin{tabular}{ccccc} \hline
Col-1 & Col-2 & Col-3 & Col-4 & Col-5 \\ \hline
0.8 & 0.42 & -0.494 & -0.5716 & 0.06804 \\
-0.7 & 0.55 & -0.977 & -2.6457 & -0.49711 \\
1.2 & -0.15 & -0.209 & -1.1252 & -0.26397 \\ \hline
\end{tabular}
\end{center}
\end{table}
%
> y
# 単に latex クラスのオブジェクトを書くと,print.latex をデフォルトパラメータで呼び出すのと同じ
%
\ctable[
cap = {},
caption = {},
label = ,
pos = htbp
]
{ccccc}
{}
{ \FL
Col-1 & Col-2 & Col-3 & Col-4 & Col-5 \ML
0.766844081830774 & 0.423531957258703 & -0.494376057746704 & -0.571619830805184 & 0.0680418734516652 \NN
-0.657730380834299 & 0.54521802925664 & -0.976634479942584 & -2.64568788961159 & -0.497109023895327 \NN
1.17680670401891 & -0.153281338683701 & -0.209140384374655 & -1.12520371399254 & -0.263967400099695 \LL
}
%
直前のページへ戻る
E-mail to Shigenobu AOKI