使用 xtable 时删除 data.frame 行名 [英] Remove data.frame row names when using xtable

查看:20
本文介绍了使用 xtable 时删除 data.frame 行名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我承认这个有点紧张(读作:愚蠢"),我认为这也很容易.我正在写一份报告,我想使用 xtable 包来生成 LaTeX 表(请注意,memisc 包可以完成这项工作,但说我只想用 <代码>xtable).

OK, I admit this one is a bit uptight (read: "stupid"), and I assume it's quite easy too. I'm writing a report and I want to use xtable package for LaTeX table generation (note that memisc package does the job, but say I want to do this solely with xtable).

让我们使用标准的 mtcars 数据集和 reshape 包:

Let's use standard mtcars dataset and reshape package:

mdtf <- melt(mtcars, id.vars = c("am", "cyl"), measure.vars = c("mpg", "hp", "wt"))
( res <- cast(mdtf, am + cyl ~ variable, mean) )
  am cyl      mpg        hp       wt
1  0   4 22.90000  84.66667 2.935000
2  0   6 19.12500 115.25000 3.388750
3  0   8 15.05000 194.16667 4.104083
4  1   4 28.07500  81.87500 2.042250
5  1   6 20.56667 131.66667 2.755000
6  1   8 15.40000 299.50000 3.370000

如果我将其包装在 xtable 中,我将获得行名 (1..6):

If I wrap this inside xtable, I'll get row names (1..6):

xtable(res)
% latex table generated in R 2.13.0 by xtable 1.5-6 package
% Fri Mar 25 09:40:12 2011
egin{table}[ht]
egin{center}
egin{tabular}{rrrrrr}
  hline
 & am & cyl & mpg & hp & wt \ 
  hline
1 & 0.00 & 4.00 & 22.90 & 84.67 & 2.94 \ 
  2 & 0.00 & 6.00 & 19.12 & 115.25 & 3.39 \ 
  3 & 0.00 & 8.00 & 15.05 & 194.17 & 4.10 \ 
  4 & 1.00 & 4.00 & 28.07 & 81.88 & 2.04 \ 
  5 & 1.00 & 6.00 & 20.57 & 131.67 & 2.75 \ 
  6 & 1.00 & 8.00 & 15.40 & 299.50 & 3.37 \ 
   hline
end{tabular}
end{center}
end{table}

现在,我有没有机会避免这种情况(如果可能,单线)?

Now, is there any chance that I can avoid this (in one-liner, if possible)?

推荐答案

print 方法中使用 include.rownames=FALSE.见 ?print.xtable:

Use include.rownames=FALSE in the print method. See ?print.xtable:

R> print(xtable(res), include.rownames=FALSE)

% latex table generated in R 2.12.2 by xtable 1.5-6 package
% Fri Mar 25 10:06:08 2011
egin{table}[ht]
egin{center}
egin{tabular}{rrrrr}
  hline
am & cyl & mpg & hp & wt \ 
  hline
0.00 & 4.00 & 22.90 & 84.67 & 2.94 \ 
  0.00 & 6.00 & 19.12 & 115.25 & 3.39 \ 
  0.00 & 8.00 & 15.05 & 194.17 & 4.10 \ 
  1.00 & 4.00 & 28.07 & 81.88 & 2.04 \ 
  1.00 & 6.00 & 20.57 & 131.67 & 2.75 \ 
  1.00 & 8.00 & 15.40 & 299.50 & 3.37 \ 
   hline
end{tabular}
end{center}
end{table}

这篇关于使用 xtable 时删除 data.frame 行名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆