1

I have looked at other answers on this subject but can't seem to get it to work - I am trying not show any decimal places in my output table but they keep appearing. Using knitr:

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

The values are numeric:

   '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

I did try digits=c(0,0,0) in case I needed to allow for the rownames but it made no difference - the values always have two decimal places.

Thanks

UPDATE

Thank you for the answers, they both helped me solve it. I was mixing up the arguments of xtable and print.xtable.

So now in my script file I have

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

and in my R markdown file

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

which works

Community
  • 1
  • 1
user3387656
  • 93
  • 1
  • 1
  • 7
  • 3
    `print.xtable` does not have a `digits` argument and thus ignores it. `xtable(cyclist_proportions, digits=0)` should do the trick. – fabians Mar 06 '14 at 11:05
  • possible duplicate of [Control number of decimal points on xtable output in R](http://stackoverflow.com/questions/15487421/control-number-of-decimal-points-on-xtable-output-in-r) – Henrik Mar 06 '14 at 12:04
  • Not a duplicate (I lnked to the same page in my first post) – user3387656 Mar 06 '14 at 19:02
  • I saw that, but you didn't follow the advice given in that answer, i.e. `digits` as argument in `xtable`. – Henrik Mar 06 '14 at 19:37

1 Answers1

1

I use

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)

for making nice tables of dataframes, where [3] counter-intuitively indexes the second column of the dataframe.

r.bot
  • 5,309
  • 1
  • 34
  • 45