【问题标题】:xtable R - can't get rid of decimal placesxtable R - 不能去掉小数位
【发布时间】:2014-03-06 10:40:13
【问题描述】:

我在这个主题上查看了other answers,但似乎无法让它工作 - 我试图在我的输出表中不显示任何小数位,但它们一直出现。使用 knitr:

{r cyclist_proportions, echo=FALSE, results='asis', warning=FALSE}
print(xtable(cyclist_proportions), digits=c(0,0), type='html', include.rownames=FALSE)

值是数字:

   'data.frame':    5 obs. of  2 variables:
 $ Percentage of all casualties that were cyclists: num  7 8 8 9 10
 $ Year                                           : num  2008 2009 2010 2011 2012

我确实尝试了 digits=c(0,0,0) 以防我需要允许行名,但它没有区别 - 值总是有两位小数。

谢谢

更新

感谢您的回答,他们都帮助我解决了这个问题。我混淆了 xtable 和 print.xtable 的参数。

所以现在在我的脚本文件中

cyclist_proportionstab <- xtable(cyclist_proportions)
digits(cyclist_proportionstab) <- 0

在我的 R markdown 文件中

```{r cyclist_proportions, echo=FALSE, results='asis', warning=FALSE}
print.xtable(cyclist_proportionstab, include.rownames = FALSE, type='html')
```

哪个有效

【问题讨论】:

  • print.xtable 没有 digits 参数,因此忽略它。 xtable(cyclist_proportions, digits=0) 应该可以解决问题。
  • 不是重复的(我在第一篇文章中链接到同一页面)
  • 我看到了,但您没有遵循该答案中给出的建议,即digits 作为xtable 中的参数。

标签: r knitr xtable


【解决方案1】:

我用

summtab <- xtable(summdf, caption = "Number of obs by some other variable")
names(summtab)<-c("Categorical var", "n obs", "Per cent")
digits(summtab)[3] <- 0
print(summtab, include.rownames = FALSE, booktabs = TRUE, sanitize.text.function = identity)

用于制作漂亮的数据帧表,其中 [3] 反直觉地索引数据帧的第二列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多