找回密码
 注册
查看: 3741|回复: 0

R语言 xtable包 xtable()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-10-2 07:24:00 | 显示全部楼层 |阅读模式
xtable(xtable)
xtable()所属R语言包:xtable

                                        Create Export Tables
                                         创建导出表

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

Function converting an R object to an xtable object, which can then be printed as a LaTeX or HTML table.
功能转换的R对象xtable对象,然后可以打印乳胶或HTML表。


用法----------Usage----------


xtable(x, caption=NULL, label=NULL, align=NULL, digits=NULL,
       display=NULL, ...)



参数----------Arguments----------

参数:x
An R object of class found among methods(xtable).  See below on how to write additional method functions for xtable.
发现其中methods(xtable)R的类的对象。见下面功能xtable如何编写额外的方法。


参数:caption
Character vector of length 1 or 2 containing the table's caption or title.  If length 2, the second item  is the "short caption" used when LaTeX generates a "List of Tables". Set to NULL to suppress the caption.  Default value is NULL.
字符向量长度为1或2包含表的标题或标题。如果长度为2,第二项是“短标题”时使用LaTeX的产生“名单表”。设置为NULL抑制的标题。默认值为NULL。


参数:label
Character vector of length 1 containing the LaTeX label or HTML anchor. Set to NULL to suppress the label.  Default value is NULL.
字符向量长度为1的含有LaTeX的标签或HTML锚。设置为NULL到抑制的标签。默认值为NULL。


参数:align
Character vector of length equal to the number of columns of the resulting table indicating the alignment of the corresponding columns.  Also, "|" may be used to produce vertical lines between columns in LaTeX tables, but these are effectively ignored when considering the required length of the supplied vector.  If a character vector of length one is supplied, it is split as strsplit(align,"")[[1]] before processing. Since the row names are printed in the first column, the length of align is one greater than ncol(x) if x is a data.frame. Use "l", "r", and "c" to denote left, right, and center alignment, respectively.  Use "p\{3cm\}" etc for a LaTeX column of the specified width. For HTML output the "p" alignment is interpreted as "l", ignoring the width request. Default depends on the class of x.
的列的数目所得到的表指示的相应的列的取向的长度等于字符向量。另外"|"可以用于产生LaTeX的表中的列之间的垂直线,但这些都有效地忽略时,考虑到所需的长度所提供的矢量。如果一个字符向量的长度为一,它会被分拆为strsplit(align,"")[[1]]前处理。由于行名称打印在第一列中,align的长度是一个比一个大ncol(x)如果x是data.frame。使用"l","r"和"c"表示,分别左,右,居中对齐。使用"p\{3cm\}"等LaTeX的列指定的宽度。对于HTML输出"p"对准被解释为"l",忽略了宽度要求。默认取决于的类的x。


参数:digits
Numeric vector of length equal to one (in which case it will be replicated as necessary) or to the number of columns of the resulting table or matrix of the same size as the resulting table  indicating the number of digits to display in the corresponding columns. Since the row names are printed in the first column, the length of the  vector digits or the number of columns of the matrix digits is one greater than ncol(x) if x is a data.frame. Default depends of class of x. If values of digits are negative, the corresponding values  of x are displayed in scientific format with abs(digits) digits.
数值向量的长度等于1(在这种情况下,它将被复制在必要时),或指示在相应的列中的要显示的位数的数目所得到的表相同的大小所得到的表或矩阵的列的数量。由于被打印在第一列中的行的名称,长度向量digits或数列的矩阵digits是一个大于ncol(x)如果x是一个data.frame。默认取决于类的x。如果值digits是否定的,相应的值x科学格式显示在abs(digits)数字。


参数:display
Character vector of length equal to the number of columns of the resulting table indicating the format for the corresponding columns. Since the row names are printed in the first column, the length of display is one greater than ncol(x) if x is a data.frame. These values are passed to the formatC function.  Use "d" (for integers), "f", "e", "E", "g", "G", "fg" (for reals), or "s" (for strings). "f" gives numbers in the usual xxx.xxx format;  "e" and "E" give n.ddde+nn or n.dddE+nn (scientific format); "g" and "G" put x[i] into scientific format only if it saves space to do so.  "fg" uses fixed format as "f", but digits as number of significant digits.  Note that this can lead to quite long result strings.  Default depends on the class of x.
的指示对应的列的格式生成的表的数目的列的长度等于字符向量。由于行名称打印在第一列中,display的长度是一个比一个大ncol(x)如果x是data.frame。这些值是通过formatC功能。使用"d"(整数),"f","e","E","g","G","fg"(实数)或"s"(字符串)。 "f"给出了数字通常是xxx.xxx格式,“"e"和"E"给n.ddde+nn或n.dddE+nn(科学),”"g"和"G"把x[i]成科学格式,仅当它这样做节省了空间。 "fg"使用固定格式"f",但digits作为的显著位数。需要注意的是,这可能会导致相当长的结果字符串。默认取决于的类的x。


参数:...
Additional arguments.  (Currently ignored.)
其他参数。 (目前忽略不计。)


Details

详细信息----------Details----------

This function extracts tabular information from x and returns an object of class "xtable". The nature of the table generated depends on the class of x. For example, aov objects produce ANOVA tables while data.frame objects produce a table of the entire data.frame.  One can optionally provide a caption (called a title in HTML) or label (called an anchor in HTML), as well as formatting specifications.  Default values for align, digits, and display are class dependent.
此功能从x和提取表格信息类"xtable"返回一个对象。生成的表的性质取决于类x。例如,aov对象产生变异数分析表,而data.frame的物体产生一个表的整个数据框。一个可以选择性地提供一个标题(称为HTML中的标题)或标签(称为在HTML中的锚),以及格式规范。默认值align,digits和display类别相关的。

The available method functions for xtable are given by methods(xtable). Users can extend the list of available classes by writing methods for the generic function xtable. These methods functions should have x as their first argument with additional arguments to specify caption, label, align, digits, and display.  Optionally, other arguments may be present to specify how the object x should be manipulated. All method functions should return an object whose class if given by c("xtable","data.frame"). The resulting object can have attributes caption and label, but must have attributes align, digits, and display.  It is strongly recommened that you set these attributes through the  provided replacement functions as they perform validity checks.
可用的方法xtable功能由methods(xtable)。用户可以扩展列表,可用的类的通用函数xtable的写作方法。这些方法的功能应该有x额外的参数作为第一个参数指定了caption,label,align,digits和display。或者,其他参数可能是存在指定对象x被操纵。所有方法函数应该返回一个对象所属的类如由c("xtable","data.frame")。对象可以有属性caption和label,但必须有属性align,digits和display。强烈recommened的,你设置这些属性,因为他们所提供的替换功能进行有效性检查。


值----------Value----------

An object of class "xtable" which inherits the data.frame class and contains several additional attributes specifying the table formatting options.
对象的类"xtable"继承data.frame类,包含了一些额外的属性,指定表的格式设置选项。


(作者)----------Author(s)----------


David Dahl <a href="mailto:dahl@stat.tamu.edu">dahl@stat.tamu.edu</a> with contributions and suggestions from many others (see source code).



参见----------See Also----------

print.xtable, caption, label,
print.xtable,caption,label,


实例----------Examples----------



## Load example dataset[#加载示例数据集]
data(tli)

## Demonstrate data.frame[#演示数据框]
tli.table <- xtable(tli[1:20,])
digits(tli.table)[c(2,6)] <- 0
print(tli.table)
print(tli.table,type="html")

## Demonstrate data.frame with different digits in cells[演示数据框与单元不同的数字]
tli.table <- xtable(tli[1:20,])
digits(tli.table) <- matrix( 0:4, nrow = 20, ncol = ncol(tli)+1 )
print(tli.table)
print(tli.table,type="html")

## Demonstrate matrix[#证明矩阵]
design.matrix <- model.matrix(~ sex*grade, data=tli[1:20,])
design.table <- xtable(design.matrix)
print(design.table)
print(design.table,type="html")

## Demonstrate aov[#展示AOV]
fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
fm1.table <- xtable(fm1)
print(fm1.table)
print(fm1.table,type="html")

## Demonstrate lm[#展示LM]
fm2 <- lm(tlimth ~ sex*ethnicty, data=tli)
fm2.table <- xtable(fm2)
print(fm2.table)
print(fm2.table,type="html")
print(xtable(anova(fm2)))
print(xtable(anova(fm2)),type="html")
fm2b <- lm(tlimth ~ ethnicty, data=tli)
print(xtable(anova(fm2b,fm2)))
print(xtable(anova(fm2b,fm2)),type="html")

## Demonstrate glm[#证明GLM]
fm3 <- glm(disadvg ~ ethnicty*grade, data=tli, family=binomial())
fm3.table <- xtable(fm3)
print(fm3.table)
print(fm3.table,type="html")
print(xtable(anova(fm3)))
print(xtable(anova(fm3)),type="html")

## Demonstrate aov[#展示AOV]
## Taken from help(aov) in R 1.1.1[#帮助(AOV)在R 1.1.1]
## From Venables and Ripley (1997) p.210.[#,从维纳布尔斯和里普利(1997年)第210页。]
N <- c(0,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,1,0,0)
P <- c(1,1,0,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,0,1,1,0)
K <- c(1,0,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,0,1,1,1,0,1,0)
yield <- c(49.5,62.8,46.8,57.0,59.8,58.5,55.5,56.0,62.8,55.8,69.5,55.0,
           62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0)
npk <- data.frame(block=gl(6,4), N=factor(N), P=factor(P), K=factor(K), yield=yield)
npk.aov <- aov(yield ~ block + N*P*K, npk)
op <- options(contrasts=c("contr.helmert", "contr.treatment"))
npk.aovE <- aov(yield ~  N*P*K + Error(block), npk)
options(op)

summary(npk.aov)
print(xtable(npk.aov))
print(xtable(anova(npk.aov)))
print(xtable(summary(npk.aov)))

summary(npk.aovE)
print(xtable(npk.aovE),type="html")
print(xtable(summary(npk.aovE)),type="html")

## Demonstrate lm[#展示LM]
## Taken from help(lm) in R 1.1.1[#在R 1.1.1从帮助(LM)]
## Annette Dobson (1990) "An Introduction to Generalized Linear Models".[吕秀莲多布森(1990),“广义线性模型”。]
## Page 9: Plant Weight Data.[#第9页:植物重量数据。]
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
print(xtable(lm.D9))
print(xtable(anova(lm.D9)))

## Demonstrate glm[#证明GLM]
## Taken from help(glm) in R 1.1.1[#帮助(GLM)在R 1.1.1]
## Annette Dobson (1990) "An Introduction to Generalized Linear Models".[吕秀莲多布森(1990),“广义线性模型”。]
## Page 93: Randomized Controlled Trial :[#第93页:随机对照试验:]
counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
d.AD <- data.frame(treatment, outcome, counts)
glm.D93 <- glm(counts ~ outcome + treatment, family=poisson())
print(xtable(glm.D93,align="r|llrc"))
print(xtable(anova(glm.D93)),hline.after=c(1),size="small")

## Demonstration of additional formatC() arguments.[示范额外的formatC()参数。]
print(fm1.table, format.args=list(big.mark = "'", decimal.mark = ","))

## Demonstration of "short caption" support.[#“短标题为”支持的示范。]
fm1sc <- aov(tlimth ~ sex + ethnicty + grade, data=tli)
fm1sc.table <- xtable(fm1sc,
  caption=c("ANOVA Model with Predictors Sex, Ethnicity, and Grade",
    "ANOVA: Sex, Ethnicity, Grade"))
print(fm1sc.table)

## Demonstration of longtable support.[#示范longtable支持。]
## Remember to insert \usepackage{longtable} on your LaTeX preamble[#记得插入\ usepackage的{longtable}你的LaTeX序言]
x <- matrix(rnorm(1000), ncol = 10)
x.big <- xtable(x,label='tabbig',caption='Example of longtable spanning several pages')
print(x.big,tabular.environment='longtable',floating=FALSE)
x <- x[1:30,]
x.small <- xtable(x,label='tabsmall',caption='regular table env')
print(x.small)  # default, no longtable [默认,没有longtable的]

## Demonstration of sidewaystable support.[#示范sidewaystable支持。]
## Remember to insert \usepackage{rotating} on your LaTeX preamble[#请记住,插入\ usepackage的{旋转}你的LaTeX序言]
print(x.small,floating.environment='sidewaystable')

if(require(stats,quietly=TRUE)) {
  ## Demonstrate prcomp[#展示prcomp]
  ## Taken from help(prcomp) in mva package of R 1.1.1[#MVA包的R 1.1.1从帮助(prcomp)]
  data(USArrests)
  pr1 <- prcomp(USArrests)
  print(xtable(pr1))
  print(xtable(summary(pr1)))

#  ## Demonstrate princomp[##展示主成分法]
#  ## Taken from help(princomp) in mva package of R 1.1.1[##的R 1.1.1 MVA包帮助(主成分法)]
#  pr2 &lt;- princomp(USArrests)[PR2 < - 主成分法(USArrests)]
#  print(xtable(pr2))[打印(xtable(PR2))]
}

## Demonstrate include.rownames, include.colnames, [#展示include.rownames,include.colnames,]
## only.contents and add.to.row arguments[#only.contents和add.to.row参数]
set.seed(2345)
res <- matrix(sample(0:9, size=6*9, replace=TRUE), ncol=6, nrow=9)
xres <- xtable(res)
digits(xres) <- rep(0, 7)
addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- c(0, 2)
addtorow$pos[[2]] <- 4
addtorow$command <- c('\vspace{2mm} \n', '\vspace{10mm} \n')
print(xres, add.to.row=addtorow, include.rownames=FALSE, include.colnames=TRUE,
  only.contents=TRUE, hline.after=c(0, 0, 9, 9))

## Demostrate include.rownames, include.colnames, [#的 - 演示include.rownames,include.colnames,]
## only.contents and add.to.row arguments in Rweave files[的#only.contents和add.to.row参数在Rweave文件]

## Not run: [#不运行:]
\begin{small}
\setlongtables
\begin{longtable}{
<<results=tex,fig=FALSE>>=
cat(paste(c('c', rep('cc', 34/2-1), 'c'), collapse='@{\hspace{2pt}}'))
@
}
\hline
\endhead
\hline
\endfoot
<<results=tex,fig=FALSE>>=
library(xtable)
set.seed(2345)
res <- matrix(sample(0:9, size=34*90, replace=TRUE), ncol=34, nrow=90)
xres <- xtable(res)
digits(xres) <- rep(0, 35)
addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- c(seq(4, 40, 5), seq(49, 85, 5))
addtorow$pos[[2]] <- 45
addtorow$command <- c('\vspace{2mm} \n', '\newpage \n')
print(xres, add.to.row=addtorow, include.rownames=FALSE, include.colnames=FALSE,
   only.contents=TRUE, hline.after=NULL)
@
\end{longtable}
\end{small}

## End(Not run)[#(不执行)]

## Demonstrate sanitization[#展示消毒]
mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)
rownames(mat) <- "$y_{t-1}$"
colnames(mat) <- c("$R^2$", "$\\bar{R}^2$", "F-stat", "S.E.E", "DW")
print(xtable(mat), type="latex", sanitize.text.function = function(x){x})

## Demonstrate booktabs[#展示booktabs功能]
print(tli.table)
print(tli.table , hline.after=c(-1,0))
print(tli.table , hline.after=NULL)
print(tli.table ,  add.to.row=list(pos=list(2), command=c("\vspace{2mm} \n")))

print(tli.table , booktabs=TRUE)
print(tli.table , booktabs=TRUE, hline.after=c(-1,0))
print(tli.table , booktabs=TRUE, hline.after=NULL)
print(tli.table , booktabs=TRUE,
  add.to.row=list(pos=list(2), command=c("\vspace{2mm} \n")))
print(tli.table , booktabs=TRUE, add.to.row=list(pos=list(2),
  command=c("youhou\n")),tabular.environment = "longtable")




转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2024-11-24 09:00 , Processed in 0.023724 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表