knitr - strange behaviour for digits - r

I ran into some trouble concerning the number of digits printed in knitr.
The number does not correspond to the settings [options('digits')].
I know that it was an issue with that about a year ago but has been resolved (https://github.com/yihui/knitr/issues/120).
```{r}
packageVersion("knitr")
options("digits")
a <- 100.101
a
as.character(a)
options(digits=4)
a
options(digits=10)
a
```
This is what I get (the same on two different machines): http://rpubs.com/markheckmann/6715 .
Something is going wrong here and I do not have a clue. Any ideas?

I don't think options(digits=10) is doing what you exepct. Perhaps you meant
sprintf( "%.10f",101.101)
# [1] "101.1010000000"

This isn't a knitr issue; it's just how R displays digits. Try your code on its own, without knitting.
a <- 100.101
a
#[1] 100.101
as.character(a)
#[1] "100.101"
options(digits=4)
a
#[1] 100.1
options(digits=10)
a
[1] 100.101
print doesn't pad numbers with zeroes to make up the width; for that you need format.
format(a, nsmall = 10)
#[1] "100.1010000000"

Related

How do I force R to write out scientific notation with 3 significant figures after a decimal?

Because Fortran, I need to write a program that, among other wonderful features, will force R to output a number in scientific notation with 3 significant figures after the decimal point.
Specifically, I need 170.5 to be written to output as 1.705e+02.
Using options(scipen = -999), I can force r to write 170.5 as 1.71e+02. However, this is insufficient for my use - ireally need that third digit after the period.
I think this question ought to be easy, but I am unable to figure out how to solve it. Thanks.
Perhaps try
options(scipen = -999, digits = 4)
Look at either sprintf or formatC.
sprintf("%s", 170.5)
[1] "1.705e+02"
print( sprintf("%s", 170.5), quote=FALSE)
[1] 1.705e+02

format of rounded large numbers in R

Starting with R (3.3.1 64bit on Windows) I found out that mean() provides too many fractional digits, so I used round(x, 1) to trim those. While that works for smaller numbers, somewhat larger numbers are output in a strange format that does not obey the rounding rules (IMHO):
I see an output of 1.330710e+04. Obviously that number should be 13307.1; in the format shown, there are actually two fractional digits displayed.
Is there a way to get more beautiful formatting? Did I make a mistake?
> format(round(345678998766.01))
[1] "3.45679e+11"
> format(round(345678998766.01), digits = 10)
[1] "345678998766"
> format(round(mean(c(345678998766.01, 345678998766.01))))
[1] "3.45679e+11"
> format(round(mean(c(345678998766.01, 345678998766.01))), digits = 10)
[1] "345678998766"

Rounding error R

When I run this calculation: 2.36*7299.83 in R is returning 17227.599 but the correct answer should be 17227.5988. Even when I run options(digits=8) before or round(2.36*7299.83, 4) it still gets the wrong answer.
It does not look like floating point error, because it is in the fourth decimal place.
Any help?
Thank you.
I think you have to specify the format:
formatC(2.36*7299.83, digits=4, format = "f")
"17227.5988"
formatC(2.36*7299.83, digits=8, format = "f")
"17227.59880000"
the default digits is 7.
getOption("digits")
# [1] 7
print(round(2.36*7299.83, 4), digits=9)
# [1] 17227.5988

How to remove leading "0." in a numeric R variable

How can one concisely change a numeric R variable (keeping it numeric) so that, e.g.,
"-0.34" becomes simply "-.34"?
Only when you output a numeric value do you have to choose a concrete representation (i.e., how the number should be formatted). You cannot change a numeric variable from "-0.34" to "-.34"; both are representations for the same number.
However, when you output an expression e, you can choose how it should be formatted. I don't know of any build-in way to leave off the leading "0", but you could always just remove it manually:
> sub("^(-?)0.", "\\1.", sprintf("%.2f", -0.34))
[1] "-.34"
You can define a function for convenience, e.g.,
numformat <- function(val) { sub("^(-?)0.", "\\1.", sprintf("%.2f", val)) }
In addition to the existing answers, I wanted to mention that the package weights has a function rd() which can be used to "round numbers to text with no leading zero". Of course, the result is not numeric but character.
library("weights")
rd(-0.341, digits=2)
[1] "-.34"
I needed to show numbers to 3 decimal places.
If you want to print to an arbitrary number of decimal places and you don't want to have to add another package (i.e., the weights package above), then this function (adapted from #stefan's answer) seems to work:
numformat <- function(x, digits = 2) {
ncode <- paste0("%.", digits, "f")
sub("^(-?)0.", "\\1.", sprintf(ncode, x))
}
So for example:
> numformat(-.232, 2)
[1] "-.23"
> numformat(-.232, 3)
[1] "-.232"
> numformat(-.232, 4)
[1] "-.2320"
In addition to #stefan's nice answer, I stumbled upon the following code which accomplishes the same thing but prints out more decimal places:
f = function(X1)gsub("0\\.","\\.", X1)
If it's for reporting in R Markdown I use the package MOTE with the function apa() and code: apa(-0.34, 2, FALSE) this will return -.34 in my documents.

Comma separator for numbers in R?

Is there a function in R to display large numbers separated with commas?
i.e., from 1000000 to 1,000,000.
You can try either format or prettyNum, but both functions return a vector of characters. I'd only use that for printing.
> prettyNum(12345.678,big.mark=",",scientific=FALSE)
[1] "12,345.68"
> format(12345.678,big.mark=",",scientific=FALSE)
[1] "12,345.68"
EDIT: As Michael Chirico says in the comment:
Be aware that these have the side effect of padding the printed strings with blank space, for example:
> prettyNum(c(123,1234),big.mark=",")
[1] " 123" "1,234"
Add trim=TRUE to format or preserve.width="none" to prettyNum to prevent this:
> prettyNum(c(123,1234),big.mark=",", preserve.width="none")
[1] "123" "1,234"
> format(c(123,1234),big.mark=",", trim=TRUE)
[1] "123" "1,234"
See ?format:
> format(1e6, big.mark=",", scientific=FALSE)
[1] "1,000,000"
>
The other answers posted obviously work - but I have always used
library(scales)
label_comma()(1000000)
I think Joe's comment to MatthewR offers the best answer and should be highlighted:
As of Sept 2018, the scales package (part of the Tidyverse) does exactly this:
> library(scales)
> x <- 10e5
> comma(x)
[1] "1,000,000"
The scales package appears to play very nicely with ggplot2, allowing for fine control of how numerics are displayed in plots and charts.

Resources