4

如果我有以下称为结果的数据框

> result
     Name       CV      LCB       UCB
1  within 2.768443 1.869964  5.303702
2 between 4.733483 2.123816 18.551051
3   total 5.483625 3.590745 18.772389

> dput(result,"")
structure(list(Name = structure(c("within", "between", "total"
), .rk.invalid.fields = list(), .Label = character(0)), CV = c(2.768443, 
4.733483, 5.483625), LCB = c(1.869964, 2.123816, 3.590745), UCB = c(5.303702, 
18.551051, 18.772389)), .Names = c("Name", "CV", "LCB", "UCB"
), row.names = c(NA, 3L), class = "data.frame")

很好地呈现这些数据的最佳方式是什么?理想情况下,我想要一个可以粘贴到报告中的图像文件,或者可能是一个 HTML 文件来表示表格?

设置有效数字的加分。

4

4 回答 4

8

我会使用xtable。我通常将它与 Sweave 一起使用。

library(xtable)
d <- data.frame(letter=LETTERS, index=rnorm(52)) 
d.table <- xtable(d[1:5,])
print(d.table,type="html")

如果你想在 Sweave 文档中使用它,你可以像这样使用它:

<<label=tab1,echo=FALSE,results=tex>>=
xtable(d, caption = "Here is my caption", label = "tab:one",caption.placement = "top")
@
于 2009-09-10T21:15:53.967 回答
7

对于表格方面,我想到了xtable包,因为它可以生成 LaTeX 输出(您可以通过 Sweave 将其用于专业报告)以及 html。

如果您在 Sweave 中将其与精美的图表结合起来(请参阅其他问题以获取 ggplot 示例),您就快到了。

于 2009-09-10T21:07:52.757 回答
2
library(ggplot2)
ggplot(result, aes(x = Name, y = CV, ymin = LCB, ymax = UCB)) + geom_errorbar() + geom_point()
ggplot(result, aes(x = Name, y = CV, ymin = LCB, ymax = UCB)) + geom_pointrange()
于 2009-09-10T21:07:29.543 回答
0

要设置有效数字,最简单的方法(对于此示例数据,请注意)是将 Name 移动到 rownames 并舍入整个事物。

#Set the rownames equal to Name - assuming all unique
rownames(result) <- result$Name  
#Drop the Name column so that round() can coerce
#result.mat to a matrix
result.mat <- result[ , -1]       
round(result.mat, 2) #Where 2 = however many sig digits you want.            

这不是一个非常强大的解决方案 - 我认为非唯一的 Name 值会破坏它,就像其他非数字列一样。但是对于生成像您的示例这样的表格,它可以解决问题。

于 2009-09-10T22:24:05.160 回答